JspSmartUpload 实现上传
来源:互联网 发布:golang mongodb连接池 编辑:程序博客网 时间:2024/06/06 02:33
作用:将全部上传文件保存到指定目录下,并返回保存的文件个数。
原型:public int save(String destPathName)
和public int save(String destPathName,int option)
其中,destPathName为文件保存目录,option为保存选项,它有三个值,分别是SAVE_PHYSICAL,SAVE_VIRTUAL和SAVE_AUTO。(同File类的saveAs方法的选项之值类似)SAVE_PHYSICAL指示组件将文件保存到以操作系统根目录为文件根目录的目录下,SAVE_VIRTUAL指示组件将文件保存到以Web应用程序根目录为文件根目录的目录下,而SAVE_AUTO则表示由组件自动选择。
注:save(destPathName)作用等同于save(destPathName,SAVE_AUTO)。
<form method="post" action="uploadfile.jsp" enctype="multipart/form-data"><input type="file" name="file" size="50"> </form>
这里enctype="multipart/form-data"是form的MIME编码,这个参数才可以上传或下载文件。
<%mySmartUpload.initialize(pageContext); //执行初始化操作 mySmartUpload.upload(); //upload file dataint size = 1024 * 1024 * 1024;if (mySmartUpload.getFiles().getSize() > size) {out.println("the files have to be < 1024MB !");} else {try {mySmartUpload.save("/Upload");out.print("成功上传文件! ");} catch (Exception e) {out.print(e.toString());}}%>
这里通过save()方法,将文件上传到根目录的Upload文件夹中。
1、saveAs作用:将文件换名另存。
原型:
public void saveAs(java.lang.String destFilePathName)
或
public void saveAs(java.lang.String destFilePathName, int optionSaveAs)
其中,destFilePathName是另存的文件名,optionSaveAs是另存的选项,该选项有三个值,分别是SAVEAS_PHYSICAL,SAVEAS_VIRTUAL,SAVEAS_AUTO。SAVEAS_PHYSICAL表明以操作系统的根目录为文件根目录另存文件,SAVEAS_VIRTUAL表明以Web应用程序的根目录为文件根目录另存文件,SAVEAS_AUTO则表示让组件决定,当Web应用程序的根目录存在另存文件的目录时,它会选择SAVEAS_VIRTUAL,否则会选择SAVEAS_PHYSICAL。
例如,saveAs("/upload/sample.zip",SAVEAS_PHYSICAL)执行后若Web服务器安装在C盘,则另存的文件名实际是c:\upload\sample.zip。而saveAs("/upload/sample.zip",SAVEAS_VIRTUAL)执行后若Web应用程序的根目录是webapps/jspsmartupload,则另存的文件名实际是webapps/jspsmartupload/upload/sample.zip。saveAs("/upload/sample.zip",SAVEAS_AUTO)执行时若Web应用程序根目录下存在upload目录,则其效果同saveAs("/upload/sample.zip",SAVEAS_VIRTUAL),否则同saveAs("/upload/sample.zip",SAVEAS_PHYSICAL)。
建议:对于Web程序的开发来说,最好使用SAVEAS_VIRTUAL,以便移植。
SAVEAS_PHYSICAL 是绝对路径,SAVEAS_VIRTUAL是相对路径(相当于前面加上Tomcat/Webapps/YourProject/)。
<%mySmartUpload.initialize(pageContext); //initiatemySmartUpload.upload(); //upload file dataint size = 1024 * 1024 * 1024;if (mySmartUpload.getFiles().getSize() > size) {//control the size of the fileout.println("the files have to be < 1024MB !");} else {try {for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {//iterating the filesFile file = mySmartUpload.getFiles().getFile(i);if (file.isMissing())continue;String virtualPath = "/Upload/";//Tomcat/webapps/YourProject/Uploadfile.saveAs(virtualPath + file.getFileName(),mySmartUpload.SAVE_VIRTUAL);}out.print("成功上传文件! ");} catch (Exception e) {out.print(e.toString());}}%>
上述代码使用了SaveAs方法,其中SAVEAS_VIRTUAL,存放到Web项目中的,Upload文件夹中。
下面的代码使用了SAVEAS_PHYSICAL,和上面的代码相同功能,其中 pageContext.getServletContext().getRealPath("/")来获得Webapps/Project的路径。
<%mySmartUpload.initialize(pageContext); //initiatemySmartUpload.upload(); //upload file dataint size = 1024 * 1024 * 1024;if (mySmartUpload.getFiles().getSize() > size) {//control the size of the fileout.println("the files have to be < 1024MB !");} else {try {for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {//iterating the filesFile file = mySmartUpload.getFiles().getFile(i);if (file.isMissing())continue;String physicalPath = pageContext.getServletContext()//Tomcat/webapps/YourProject/Upload.getRealPath("/") + "/Upload/";file.saveAs(physicalPath + file.getFileName(),mySmartUpload.SAVE_PHYSICAL);}out.print("成功上传文件! ");} catch (Exception e) {out.print(e.toString());}}%>
- jspSmartUpload实现上传现在
- jspSmartUpload实现上传现在
- JspSmartUpload 实现上传
- jspSmartUpload插件实现图片上传
- 怎样用JspSmartUpload实现文件上传攻略
- 使用jspSmartUpload组件实现图片的上传
- jspSmartUpload实现图片的批量上传
- 使用jspSmartUpload实现文件的上传
- 用jspSmartUpload组件实现文件上传、下载
- jspsmartupload上传
- jspsmartupload组件实现单个或多个文件上传(上)
- jspsmartupload组件实现单个或多个文件上传(下)
- 用jspsmartupload实现文件的上传和下载
- 在jsp中利用jspsmartupload实现上传文件
- Java【代码】使用jspSmartUpload实现文件上传demo
- 理论---jsp使用jspsmartupload组件实现文件上传功能
- JSP+Servlet使用jspsmartupload实现文件上传至服务器
- jspSmartUpload上传组件
- 字典序法生成排列
- static的作用
- POJ2828 Buy Tickets
- [leetcode] Same Tree
- Oracle 11gR2用exp无法导出空表的解决
- JspSmartUpload 实现上传
- Android应用启动协议
- cannot restore segment prot after reloc: Permission denied 解决方法
- 定时器0工作方式1
- Spring 3整合Quartz 2实现定时任务二:动态添加任务
- java7 笔记
- 在三通道彩图和单通道灰度图中画矩形框
- linux下 C语言随机数生成方法rand(产生随机数)
- php下实现折线图效果的代码