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)执行后若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());
			}
		}
	%>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值