struts2 拦截器上传文件

在没有使用框架时,使用fileupload 这个组件上传,用struts2 框架 ,他其实还是使用fileupload 的组件,只是把它进一步的额封装,然后直接使用拦截器Fileupload。

下面是具体的 步骤。

用到拦截器 fileUpload ,同时别忘了使用默认的拦截器<interceptor-ref name="defaultStack"/>

stuts.xml 配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
如果不配置 这就是默认 暂时存放的地址
 C:\Users\Administrator\Workspaces\MyEclipse Professional 2014\.metadata\.me_tcat7\work\Catalina\localhost\Demo\upload_49822ac6_15acf74e76c__8000_00000000.tmp
  <constant name="struts.multipart.saveDir" value="/upload"></constant>  //暂时存放的位置 
 <package name="file" namespace="/" extends="struts-default" >
  <action name="Fileupload" class="struts2.Fileupload">
     
    <result>/error.jsp</result>     
      <interceptor-ref name="fileUpload">
       <param name="allowedTypes">
       application/vnd.ms-excel,image/bmp,image/png,image/gif,image/jpeg
       </param>
    <!--配置允许上传的文件大小单位字节-->
     <param name="maximumSize">5242880</param>
    
     </interceptor-ref>
     <interceptor-ref name="defaultStack"/>
  
 </action>
</package>
</struts>    

关于jsp的上传页面 ,method 需要post,然后enctype="multipart/form-data",并且file value 与action中的file名称相同。

action

// 注意这几个文件,文件类型 和文件名

        private   File file;
private String fileFileName;
private String  fileContentType;

             String root = ServletActionContext.getServletContext().getRealPath("\\upload");

//刚开始用root 结果报错说读取不到目录
        
   InputStream is = new  FileInputStream(file);  
       //上传到服务器端的 图片保存地址
       String root3="/root/apache-tomcat-8.5.9/webapps/Picture";
       OutputStream os = new FileOutputStream(new File(root3, fileFileName));
       System.out.println("fileFileName: " + fileFileName); 
    
      
       byte[] buffer = new byte[500];
       int length = 0;
       while(-1 != (length = is.read(buffer, 0, buffer.length)))
       {
           os.write(buffer);
       }        
       os.close();
       is.close();
       return SUCCESS;
   }

文件下载 


private String path;
   
  
  
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
   public InputStream getInputStream(){
return ServletActionContext.getServletContext().getResourceAsStream(path);
  
   }
   public String down(){
  return SUCCESS;
   }







<package name="down"  namespace="/" extends="struts-default" >
  <action name="down" class="struts2.down" method="down">
  <!-- 下载资源的位置 -->
  <param name="path">/downpicture/7828.jpg</param>
  <result type="stream" name="success">
  <!-- 资源类型 -->
  <param name="contenType">image/jpg,image/png</param>
  <param name="inputName">inputStream</param>
  <!--  文件名 -->
  <param name="contentDisposition">attachment;fileName="7828.jpg"
  </param>
  <param name="bufferSize">1024*1024</param>
  
  </result>
    </action>


</package>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值