JspSmartUpload 实现上传

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 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()); } }%>


这里通过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)运行后若Webserver安装在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());
			}
		}
	%>



posted @ 2017-07-09 12:13  jhcelue  阅读(388)  评论(0编辑  收藏  举报