注: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 data int 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()); } }%>
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); //initiate mySmartUpload.upload(); //upload file data int size = 1024 * 1024 * 1024; if (mySmartUpload.getFiles().getSize() > size) {//control the size of the file out.println("the files have to be < 1024MB !"); } else { try { for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {//iterating the files File file = mySmartUpload.getFiles().getFile(i); if (file.isMissing()) continue; String virtualPath = "/Upload/";//Tomcat/webapps/YourProject/Upload file.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); //initiate mySmartUpload.upload(); //upload file data int size = 1024 * 1024 * 1024; if (mySmartUpload.getFiles().getSize() > size) {//control the size of the file out.println("the files have to be < 1024MB !"); } else { try { for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {//iterating the files File 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 实现上传,布布扣,bubuko.com
原文地址:http://blog.csdn.net/arcticfoxhan/article/details/37880611