lq147011476 发表于 2013-1-15 02:13:46

struts2的文件上传和下载

struts2的文件上传和下载
文件上传部分:
上传页面 upload.jsp
      <s:form action ="upload" method ="POST" enctype ="multipart/form-data" >   
                <s:file name ="file" label ="上传文件"/>
                <s:submit />   
      </s:form >
上传 UploadAction.java
private File file;
   
private String fileFileName;
   
private String fileContextType;
   
private String root;
   
private HttpServletRequest request;
private Map session;
   
   @Override
public String execute() throws Exception {
   
   session.put("fileFileName", fileFileName);
   
    //取文件后缀名
   String lastname=null;
    int i = fileFileName.lastIndexOf('.');
   lastname=fileFileName.substring(i+1);      
                  
    //取当前日期
   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
   Calendar calendar = Calendar.getInstance();
   String date=sdf.format(calendar.getTime());
   
    //10亿的随机数
   java.util.Random r=new java.util.Random();   
   String radom=String.valueOf(r.nextInt(1000000000));
   
   fileFileName=date+radom+"."+lastname;
   
   session.put("fname", fileFileName);
   
   InputStream is=new FileInputStream(file);
   
   root= ServletActionContext.getRequest().getRealPath("/upload") ;//存储到apache项目发布目录上
//   String root="D:\\a1/upload";//存储到项目文件夹里
   
   session.put("fpath", root);
   File copyFile=new File(root,fileFileName);
   
   OutputStream os=new FileOutputStream(copyFile);
   
    byte[] buffer=new byte;
   
    int length=0;
   
    while(   (length=is.read(buffer))   >   0   ){
       os.write(buffer, 0, length);
   }
   
   is.close();
   
   os.close();
   
    return SUCCESS;
   }
struts.xml配置
   <action name="upload"
       class="com.enorth.mpup.action.UploadAction">
      <result name="success">uploadshow.jsp</result>
      <result name="input">index.jsp</result>
    </action>
文件下载部分:
下载页面 uploadshow.jsp
    <a    href="download.action"   target="_blank">
          <%=session.getAttribute("fileFileName") %>
    </a>
下载 FileDownAction.java
private String fileName;// 初始的通过param指定的文件名属性

private String inputPath;// 指定要被下载的文件路径

private HttpServletRequest request;
   
private Map session;
   
   
public InputStream getInputStream() throws Exception {

    // 通过 ServletContext,也就是application 来读取数据

   inputPath="/upload/"+(String)session.get("fname");
   
    return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
   
   }

public String execute() throws Exception {
   
    return SUCCESS;   
   
   }

/** 提供转换编码后的供下载用的文件名 */

public String getDownloadFileName() {

   fileName=(String) session.get("fname");
   
   System.out.println(fileName);
   
   String downFileName = fileName;

    try {

       downFileName = new String(downFileName.getBytes(), "ISO8859-1");

   } catch (UnsupportedEncodingException e) {

       e.printStackTrace();

   }

    return downFileName;

   }
struts.xml配置
   <!-- 下载现有文件 -->
    <action name="download" class="com.enorth.mpup.action.FileDownAction">
   <!--   <param name="inputPath">/download/aaa.txt</param> -->   
       <!-- 初始文件名   
       <param name="fileName">aaa.txt</param>-->
      <result name="success" type="stream">
      <param name="contentType">
         application/octet-stream;charset=ISO8859-1
      </param>
      <param name="inputName">inputStream</param>
         <!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性   
         对应action类中的方法 getDownloadFileName() -->
      <param name="contentDisposition">
         attachment;filename="${downloadFileName}"
      </param>
      <param name="bufferSize">4096</param>
      </result>
    </action>
以上均为部分核心文件.


Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) -

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream

这两个错误的原因是缺少jar包.
commons-fileupload-1.2.1.jar
commons-io-1.4.jar


为了您的安全,请只打开来源可靠的网址
打开网站    取消
来自: http://hi.baidu.com/lichenblog/blog/item/25f02026da7a7b0b908f9dfc.html
页: [1]
查看完整版本: struts2的文件上传和下载