struts2 文件上传与下载 (简洁实例)

jar列表:

commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
freemarker-2.3.18.jar
ognl-3.0.4.jar
struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar[size=xx-small][/size]
javassist-3.11.0.GA.jar


Action:
         /* 上传文件存放的目录 */
	private final static String UPLOAD_URL = "/test";
	/* 上传文件的集合 */
	private List<File> file; //必须与表单元素的NAME属性值一致
	/* 上传文件名的集合 */
	private List<String> fileFileName;  
         //必须跟随文件名再追加FileName
	/* 上传文件类型的集合 */
	private List<String> fileFileContentType;  
         //必须跟随文件名再追加fileFileContentType

         /* ... 省略GET SET */

         public String uploadFile(){
		for(int i = 0; i < file.size();i++){
			try {
				this.upload(i);  //自定义方法调用
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "success";
	}
	

	private void upload(int i) throws Exception{
	[align=left][/align]	InputStream in = new FileInputStream(file.get(i));
		String dir = ServletActionContext.getServletContext().getRealPath(UPLOAD_URL);
		File uploadFile = new File(dir, this.getFileFileName().get(i)); //上传的文件
		OutputStream out = new FileOutputStream(uploadFile);
		byte [] buffer = new byte[1024*1024];
		int length;
		while((length = in.read(buffer))> 0){
			out.write(buffer,0,length);
		}
		if(in!=null)
		in.close();
		if(out!=null)
		out.close();
	}
 


Jsp:
    <!-- 文件上传需要更改enctype属性:以二进制形式进行文件上传:multipart/form-data -->
    <s:form action="你的ACTION名称.action!你要指定ACTION类中的方法" method="post" enctype="multipart/form-data">
  	 	<s:file name="file"></s:file><br/>
  	 	<s:file name="file"></s:file><br/>
  	 	<s:file name="file"></s:file><br/>
  	 	<s:submit value="开始上传"></s:submit>
  	 </s:form>


struts.xml
    <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 系统常量定义,定义上传文件字符集编码 -->
	<constant name="struts.i18n.encoding" value="gb2312"></constant>
	<!-- 系统常量定义,定义上传文件临时存放目录 -->
	<constant name="struts.multipart.saveDir" value="f:\"></constant>
         
         <!-- com.a.b.c.d.JavaClass-->
         <action name="自定义的名称" class="你的处理文件上传的ACTION类路径(需写完整的路径名:所有的包名)">
</struts>



文件下载:下载ACTION类
    private String downLoad;  //负责接收文件名
     private String inputPath;  //源文路径
    /* 属性的GET SET 省略 */

    /* 带有InputStream返回值的(get)方法 */
    public InputStream getTargetFile(){
	return ServletActionContext.getServletContext().getResourceAsStream(inputPath+downLoad);
	}

      public String execute()  {
		return "success";
	}
    


Struts.xml 配置下载action
    <action name="" class="">
                   <!-- 为action中的inputPath属性初始化值 -->
    		<param name="inputPath">/test/</param>  
                		<result type="stream">  <!-- 指定类型stream 以流形式进行传输 -->
    			<param name="contentType">image/gif</param> <!--指定文件内容类型和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片 -->
    			<param name="inputName">targetFile</param><!--对应action类中的getTargetFile方法-->
    			<param name="contentDisposition">attachment;filename="123.gif"</param>
<!--指定文件以附件形式弹出下载对话框;filename="123.gif" 指定下载对话框中显示的文件名称;
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。
如果直接写filename="123.gif",默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="123.gif"-->
    			<param name="bufferSize">4096</param> <!--指定下载缓冲区的大小-->
    		</result>
    	</action>


Jsp:
    <a href="downLoad.action?downLoad=1.gif">下载</a>  
    <!-- 传值给ACTION类中的downLoad属性,指定下载的文件名称  -->

你可能感兴趣的:(java,jsp,struts,下载,上传)