struts2文件上传与下载

先是最简单的上传

后面是配置上传文件的大小和文件类型

a.编写Action组件,定义File []xxx,String[] xxxFileName,String[] xxxContentType,添加set方法

xxx为临时文件 

       xxxFileName可以得到上传文件名                                //后面的FileName为固定写法

      xxxContentType可以得到文件上传类型    //后面的ContentType为固定写法

在相应action方法中其实就是复制文件因为临时文件会自动被删除  
b.在form表单中
  <form action="upload" method="post" enctype="multipart/form-data">

    <input type="file" name="xxx"><br/>//名字必须为XXX

<input type="file" name="xxx"><br/>

        <input type="submit" value="上传">
    </form>   


例子:
action:
public class FileUpload extends ActionSupport
{
private File[] fm;
private String[] fmFileName;
private String[] fmContentType;
@Override
 public String execute() throws Exception {
  String real=ServletActionContext.getServletContext().getRealPath("/WEB-INF/video");
  for(int i=0;i<fm.length;i++)
  {
   fmFileName[i]=UUID.randomUUID()+"_"+fmFileName[i];//防止文件重名问题
   String path=getPath(real,fmFileName[i]);//防止一个目录存放多个文件
   FileUtil.copy(fm[0], new File(path+File.separator+fmFileName[i]));
  }
  return super.execute();
 }
 private String getPath(String real, String file) {
  // TODO Auto-generated method stub
  int hashcode=file.hashCode();
  int dir1=hashcode & 0xf;
  int dir2=(hashcode & 0xf0)>>4;
  String path=real+File.separator+dir1+File.separator+dir2;
  File f=new File(path);
  if(!f.exists()){
   f.mkdirs();
  }
  return path;
 }

Util:
public class FileUtil {
public static void copy(File src, File desc) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
FileInputStream fis = new FileInputStream(src);
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(desc);
bos = new BufferedOutputStream(fos);
int size = -1;
byte[] buf = new byte[1024];
while ((size = bis.read(buf)) > 0) {
bos.write(buf, 0, size);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
}
xml:
<package name="jack" namespace="/" extends="struts-default">
<action name="upload" class="action.FileUpload">
<result>/WEB-INF/JSP/ok.jsp</result>
</action>
<action name="form">
<result>/WEB-INF/JSP/form.jsp</result>
</action>
</package>
form.jsp:
  <body>
    <form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="fm"><br/>
    <input type="file" name="fm"><br/>
<input type="submit" value="上传">
    </form>
  </body>
----在src下新建个struts.properties----
写上:struts.multipart.maxSize=2097152000    //改变文件上传大小
----action中限制上传类型和上传文件大小-----
      c.在struts.xml的action配置中,定义fileUpload拦截器的引用,并指定限定参数
      d.将Action组件继承ActionSupport,并在action配置中添加input视图
      e.在input视图中使用<s:fielderror/>标签显示提示信息<result name="input">error.jsp</result>


<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif</param>
<param name="maximumSize">2097152</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<result>/WEB-INF/JSP/ok.jsp</result>
<result name="input">/WEB-INF/JSP/error.jsp</result>
下载:

action:
        //struts.xml文件中<param name="inputName">downFile</param>必须要有
private InputStream downFile;
//接收请求参数根据传过来的id下载相应资源不是必须的
private int id;
//给配置文件传值用于显示不是必须的
private String fname;

//将要下载的资源给downFile属性
public String execute(){
//TODO 可以追加登录\扣积分\是否为会员等检查
//TODO 根据id去数据库获取文件名
fname = "a.bmp";
String destPath = ServletActionContext.getServletContext().getRealPath("/images");
String fileName = destPath+File.separatorChar+fname;
try {
downFile = new FileInputStream(fileName);
return "success";
} catch (FileNotFoundException e) {
e.printStackTrace();
return "fail";
}
}


<!-- 下载 -->
<action name="down" class="tarena.action.DownLoadAction">
<result name="success" type="stream">
<param name="inputName">downFile</param>//Action中InputStream的名字

<param name="contentDisposition">attachment;filename=${fname}</param>

//attachment没有预览效果点击直接打开下载对话框默认为inline;filename显示给用户看到的文件名字

<param name="bufferSize">1024</param>
</result>
<result name="fail">/fail.jsp</result>
</action>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值