Struts2总结--文件的上传下载

一、文件的上传

    1、表单的准备
       A:须把HTML表单的enctype属性设置为:multipart/form-data
       B:须把HTML 表单的method属性设置为Post
       C:须添加<input type="file">字段

       

<s:form action="fileUpload.do" method="post" enctype="multipart/form-data" theme="simple">
        PPTFile: <s:file name="ppt" label="PPTFile"></s:file>
        PPTDesc: <s:textfield name="pptDesc[0]" label="PPTDesc"></s:textfield>
        <br/><br/>
        
        PPTFile: <s:file name="ppt" label="PPTFile"></s:file>
        PPTDesc: <s:textfield name="pptDesc[1]" label="PPTDesc"></s:textfield>
        <br/><br/>
        
        PPTFile: <s:file name="ppt" label="PPTFile"></s:file>
        PPTDesc: <s:textfield name="pptDesc[2]" label="PPTDesc"></s:textfield>
        <br/><br/>
        <s:submit></s:submit>
    </s:form>

    2、导入commons-fileupload-1.3.jar  commons-io-2.0.1.jar,并且使用FileUpload 拦截器

   3、Action类的编写

       在 Action 中定义如下 3 个属性, 并提供对应的 getter 和 setter
        //文件对应的 File 对象
        private File [fileFieldName];
        //文件类型
        private String [fileFieldName]ContentType;
        //文件名
        private String [fileFieldName]FileName;

      并且使用IO流进行上传

     

import java.io.File;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

public class UploadAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<File> ppt;
	private List<String> pptContentType;
	private List<String> pptFileName;
	
	private List<String> pptDesc;

	
	public List<File> getPpt() {
		return ppt;
	}


	public void setPpt(List<File> ppt) {
		this.ppt = ppt;
	}


	public List<String> getPptContentType() {
		return pptContentType;
	}


	public void setPptContentType(List<String> pptContentType) {
		this.pptContentType = pptContentType;
	}


	public List<String> getPptFileName() {
		return pptFileName;
	}


	public void setPptFileName(List<String> pptFileName) {
		this.pptFileName = pptFileName;
	}


	public List<String> getPptDesc() {
		return pptDesc;
	}


	public void setPptDesc(List<String> pptDesc) {
		this.pptDesc = pptDesc;
	}


	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println(ppt);
		System.out.println(pptContentType);
		System.out.println(pptFileName);
		System.out.println(pptDesc);
		ValueStack actionContext = ActionContext.getContext().getValueStack();
		actionContext.set("aa", "aaa");
		
		/*String dir = ServletActionContext.getServletContext().getRealPath("/files/"+pptFileName);
		File file = new File(dir);
		System.out.println(dir);
		FileOutputStream out = new FileOutputStream(file);
		FileInputStream in = new FileInputStream(ppt);
		
		byte[] b = new byte[1024];
		int len = 0;
		while((len=in.read(b)) != -1){
			out.write(b, 0, len);
		}
		
		out.close();
		in.close();*/
		
		return "input";
	}

}

注:一次传多个文件:若传递多个文件, 则上述的 3 个属性, 可以改为 List 类型! 多个文件域的 name 属性值需要一致.

3、struts.xml文件配置,可以使用FileUploadInterceptor 拦截器的参数的方式来进行限制

<package name="fileUpload" extends="struts-default" namespace="/">
     
        <interceptors>
          <interceptor-stack name="zhuojing">
             <interceptor-ref name="defaultStack">
                <param name="fileUpload.maximumSize">1024*150</param>
                <param name="fileUpload.allowedTypes">image/jpeg</param>
                <param name="fileUpload.allowedExtensions">jpg,png</param>
             </interceptor-ref>
          </interceptor-stack>
        </interceptors>
        
        <default-interceptor-ref name="zhuojing"></default-interceptor-ref>
        
        <action name="fileUpload" class="zhuojing.upload.UploadAction">
           <result>/Success.jsp</result>
           <result name="input">/Upload/index.jsp</result>
        </action>
        
     </package>

二、文件的下载

1.struts.xml中使用result  的type="stream"

<package name="fileUpload" extends="struts-default" namespace="/">
     
        
        <action name="testDown" class="zhuojing.upload.DownAction">
           <result type="stream">
              <param name="bufferSize">2048</param>
           </result>
        </action>
     </package>

2、可以为 stream 的 result 设定如下参数(一般前四个需要动态写在Action中)
        contentType: 结果类型
        contentLength: 下载的文件的长度
        contentDisposition: 设定 Content-Dispositoin 响应头. 该响应头指定接应是一个文件下载类型, 一般取值为  attachment;filename="document.pdf".

        inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream(指定所需下载文件的输入流,默认名字为inputStream)

        bufferSize: 缓存的大小. 默认为 1024
        allowCaching: 是否允许使用缓存 
        contentCharSet: 指定下载的字符集 

以上参数可以在 Action 中以 getter 方法的方式提供!

    

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String contentType;
	private long contentLength;
	private String contentDisposition;
	private InputStream inputStream;
	
	
	public String getContentType() {
		return contentType;
	}


	public long getContentLength() {
		return contentLength;
	}


	public String getContentDisposition() {
		return contentDisposition;
	}


	public InputStream getInputStream() {
		return inputStream;
	}


	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
		contentType = "image/jpeg";
		contentDisposition = "attachment;filename=1.jpg";
		String path = ServletActionContext.getServletContext().getRealPath("/files/31.jpg");
		File file = new File(path);
		inputStream = new FileInputStream(file);
		contentLength = inputStream.available();
		return SUCCESS;
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值