JspSmartUpload 实现上传

来源:互联网 发布:golang mongodb连接池 编辑:程序博客网 时间:2024/06/06 02:33
2、save 
  作用:将全部上传文件保存到指定目录下,并返回保存的文件个数。 
  原型: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());}}%>



0 0