struts2上传,下载

          我用<html:file>上传文件,我想在Action中获取文件上传的全路径,然后保存在数据库中,方便后面的下载。如果全路径是构建的,我新建一个目录为upload 
把上传的文件放在目录里。
     从页面获得一个输入流,然后你要在你的代码里设置一个输出流,输出流的全路径你可以任意指定。之后文件就会保存到你设置的流的目标路径下,这个路径你可以保存在数据库
JAVA后台
public void uploadFiles() throws Exception {

  try {
   if (upload != null) {
    FunjianBean funjianBean=null;
    for (int i = 0; i < this.upload.length; i++) {
     funjianBean=new FunjianBean();
     int indes = uploadFileName[i].lastIndexOf(".");
     Date date = new Date();
     String filename = Utils.getLongNo()
       + this.uploadFileName[i].substring(indes,
         uploadFileName[i].length());
     String classpath = BaseFileAction.class.getResource("/")
       .getPath()
       .replaceAll("WEB-INF/classes/", "upload/");
     FileUtil.mkDir(classpath
       + DateFormatUtil.SIMPLE_DATE_FORMAT_yyyy
         .format(date));
     FileUtil
       .mkDir(classpath
         + DateFormatUtil.SIMPLE_DATE_FORMAT_MM
           .format(date));
     FileUtil
       .mkDir(classpath
         + DateFormatUtil.SIMPLE_DATE_FORMAT_dd
           .format(date));
     String savePath = classpath
       + DateFormatUtil.SIMPLE_DATE_FORMAT_dd.format(date)
       + "/" + filename;
     this.filePath = DateFormatUtil.SIMPLE_DATE_FORMAT_dd
       .format(date)
       + "/" + filename;
     funjianBean.setUrl(filePath);
     FileUtil.createFile(savePath, upload[i]);
     funjianBean.setTitle(this.uploadFileName[i]);
     this.funjianbeanList.add(funjianBean);
     this.fileName=this.uploadFileName[i];
    }

   }
  } catch (Exception e) {
   throw new Exception("Cannot excute basefileaction uploadFiles  "
     + e, e);
  }
 }

jsp页面

<form name="theForm" method="post" action="<%=path%>/aqyj/save.action" enctype="MULTIPART/FORM-DATA"> 
<input type="file" name="upload">
</form>


struts.xml
<package name="uploadfiles" extends="struts-default" abstract="true">
  <interceptors>
   <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor">
    <param name="allowedTypes">
     image/jpeg/jpg/bmp/txt/xls/zip
    </param>
    <param name="maximumSize">1000000000</param>
   </interceptor>
   <interceptor-stack name="fileUploadStack">
    <interceptor-ref name="fileUpload" />
   </interceptor-stack>
  </interceptors>
 </package>
然后你上传的那个包要调用此uploadfiles。
如
<package name="aqyj" namespace="/aqyj" extends="commonPackage,interceptorlogin"> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值