JAVA实现文件上传下载

Struts2实现上传下载

上传jsp页面代码:

  1. <span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <base href="<%=basePath%>">  
  10.     <title>My JSP '03.jsp' starting page</title>  
  11.     <meta http-equiv="pragma" content="no-cache">  
  12.     <meta http-equiv="cache-control" content="no-cache">  
  13.     <meta http-equiv="expires" content="0">      
  14.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  15.     <meta http-equiv="description" content="This is my page">  
  16.     <link rel="stylesheet" type="text/css" href="css/common.css" />  
  17.     <script type="text/javascript" src="js/jquery-1.11.1.js"></script>  
  18.     <script type="text/javascript">  
  19.         $(function(){  
  20.             $(".thumbs a").click(function(){  
  21.                 var largePath=$(this).attr("href");  
  22.                 var largeAlt=$(this).attr("title");  
  23.                 $("#largeImg").attr({  
  24.                     src:largePath,  
  25.                     alt:largeAlt  
  26.                 });  
  27.                 return false;  
  28.             });  
  29.         });  
  30.     </script>  
  31.   </head>  
  32.   <body>  
  33.     <h2> 文件上传</h2>  
  34.     <form action="upload.action" method="post" enctype="multipart/form-data">  
  35.         上传文件1:<input type="file" name="upload"/><br>  
  36.         上传文件2:<input type="file" name="upload"/><br>  
  37.         上传文件3:<input type="file" name="upload"/><br>  
  38.         <input type="submit" value="提交"> ${result}  
  39.     </form>  
  40.    <hr>  
  41.    <h2>图片预览</h2>  
  42.     <p><img id="largeImg" src="images/img1-lg.jpg" alt="Large Image"/></p>  
  43.     <p class="thumbs">  
  44.         <a href="images/img2-lg.jpg" title="Image2"><img src="images/img2-thumb.jpg"></a>  
  45.         <a href="images/img3-lg.jpg" title="Image3"><img src="images/img3-thumb.jpg"></a>  
  46.         <a href="images/img4-lg.jpg" title="Image4"><img src="images/img4-thumb.jpg"></a>  
  47.         <a href="images/img5-lg.jpg" title="Image5"><img src="images/img5-thumb.jpg"></a>  
  48.         <a href="images/img6-lg.jpg" title="Image6"><img src="images/img6-thumb.jpg"></a>  
  49.     </p>  
  50.   </body>  
  51. </html></span>  
struts.xml部分代码

[html] view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5. <struts>  
  6.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  7.     <constant name="struts.devMode" value="true" />  
  8.     <!-- 国际化 -->  
  9.     <constant name="struts.custom.i18n.resources" value="app"></constant>  
  10.     <package name="default" namespace="/" extends="struts-default">  
  11.             <action name="upload" class="com.lihf.action.FileUploadAction">  
  12.                     <result>/jsp/03.jsp</result>  
  13.                     <result name="input">/jsp/error.jsp</result>  
  14.                     <!-- 配置拦截器限制上传文件类型及大小 -->  
  15.                     <interceptor-ref name="fileUpload">  
  16.                         <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param>  
  17.                         <param name="maximumSize">2M</param>  
  18.                     </interceptor-ref>  
  19.                     <interceptor-ref name="defaultStack"></interceptor-ref>  
  20.             </action>  
  21.             <action name="download" class="com.lihf.action.DownLoadAction">  
  22.                 <param name="inputPath">/images/img2-lg.jpg</param>  
  23.                 <result name="success" type="stream">  
  24.                     <param name="contentType">application/octet-stream</param>  
  25.                     <param name="inputName">inputStream</param>  
  26.                     <!-- 以附件的形式下载 -->  
  27.                     <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>  
  28.                     <param name="bufferSize">8192</param>  
  29.                 </result>  
  30.             </action>  
  31.     </package>  
  32. </struts></span>  
web.xml部分代码

[html] view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>scxz</display-name>  
  4.   <servlet>  
  5.     <servlet-name>UploadServlet</servlet-name>  
  6.     <servlet-class>com.lihf.servlet.UploadServlet</servlet-class>  
  7.   </servlet>  
  8.   <servlet>  
  9.     <servlet-name>DownloadServlet</servlet-name>  
  10.     <servlet-class>com.lihf.servlet.DownloadServlet</servlet-class>  
  11.   </servlet>  
  12.   <servlet>  
  13.     <servlet-name>SmartUploadServlet</servlet-name>  
  14.     <servlet-class>com.lihf.servlet.SmartUploadServlet</servlet-class>  
  15.   </servlet>  
  16.   <servlet>  
  17.     <servlet-name>SmartDownloadServlet</servlet-name>  
  18.     <servlet-class>com.lihf.servlet.SmartDownloadServlet</servlet-class>  
  19.   </servlet>  
  20.   <servlet-mapping>  
  21.     <servlet-name>UploadServlet</servlet-name>  
  22.     <url-pattern>/uploadServlet.do</url-pattern>  
  23.   </servlet-mapping>  
  24.   <servlet-mapping>  
  25.     <servlet-name>DownloadServlet</servlet-name>  
  26.     <url-pattern>/downloadServlet.do</url-pattern>  
  27.   </servlet-mapping>  
  28.   <servlet-mapping>  
  29.     <servlet-name>SmartUploadServlet</servlet-name>  
  30.     <url-pattern>/smartUploadServlet.do</url-pattern>  
  31.   </servlet-mapping>  
  32.   <servlet-mapping>  
  33.     <servlet-name>SmartDownloadServlet</servlet-name>  
  34.     <url-pattern>/smartDownloadServlet.do</url-pattern>  
  35.   </servlet-mapping>  
  36.   <filter>  
  37.     <filter-name>struts2</filter-name>  
  38.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  39.   </filter>  
  40.   <filter-mapping>  
  41.       <filter-name>struts2</filter-name>  
  42.       <url-pattern>/*</url-pattern>  
  43.   </filter-mapping>  
  44.   <welcome-file-list>  
  45.     <welcome-file>index.jsp</welcome-file>  
  46.   </welcome-file-list>  
  47. </web-app></span>  
action方法:

[html] view plain copy
  1. <span style="font-size:18px;">package com.lihf.action;  
  2.   
  3. import java.io.File;  
  4. import java.util.List;  
  5.   
  6. import org.apache.commons.io.FileUtils;  
  7. import org.apache.struts2.ServletActionContext;  
  8.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. public class FileUploadAction extends ActionSupport {  
  12.   
  13.     private List<File> upload;  
  14.     private List<String> uploadContentType;  
  15.     private List<String> uploadFileName;  
  16.     private String result;  
  17.     public List<File> getUpload() {  
  18.         return upload;  
  19.     }  
  20.     public void setUpload(List<File> upload) {  
  21.         this.upload = upload;  
  22.     }  
  23.     public List<String> getUploadContentType() {  
  24.         return uploadContentType;  
  25.     }  
  26.     public void setUploadContentType(List<String> uploadContentType) {  
  27.         this.uploadContentType = uploadContentType;  
  28.     }  
  29.     public List<String> getUploadFileName() {  
  30.         return uploadFileName;  
  31.     }  
  32.     public void setUploadFileName(List<String> uploadFileName) {  
  33.         this.uploadFileName = uploadFileName;  
  34.     }  
  35.     public String getResult() {  
  36.         return result;  
  37.     }  
  38.     public void setResult(String result) {  
  39.         this.result = result;  
  40.     }  
  41.     @Override  
  42.     public String execute() throws Exception {  
  43.         String path = ServletActionContext.getServletContext().getRealPath("/images");  
  44.         File file = new File(path);  
  45.         if(!file.exists()){  
  46.             file.mkdir();  
  47.         }  
  48.         for(int i=0;i<upload.size();i++){              
  49.             FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));  
  50.         }  
  51.         result ="上传成功!";  
  52.         return SUCCESS;  
  53.     }  
  54.   
  55. }  
  56. </span>  
下载:

action方法:

[html] view plain copy
  1. <span style="font-size:18px;">package com.lihf.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.UnsupportedEncodingException;  
  7. import java.net.URLEncoder;  
  8.   
  9. import org.apache.commons.io.FileUtils;  
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. public class DownLoadAction extends ActionSupport {  
  15.     public String inputPath;  
  16.     public String fileName;  
  17.     public String getFileName() {  
  18.         return fileName;  
  19.     }  
  20.     public void setFileName(String fileName) {  
  21.         this.fileName = fileName;  
  22.     }  
  23.     public String getInputPath() {  
  24.         return inputPath;  
  25.     }  
  26.     public void setInputPath(String inputPath) {  
  27.         this.inputPath = inputPath;  
  28.     }  
  29.     @Override  
  30.     public String execute() throws Exception {  
  31.         return SUCCESS;  
  32.     }  
  33.     public InputStream getInputStream() throws IOException{  
  34.         String path = ServletActionContext.getServletContext().getRealPath("/images");  
  35.         String filePath = path+"\\"+fileName;  
  36.         File file = new File(filePath);  
  37.         return FileUtils.openInputStream(file);  
  38.         //根据文件路径获取流信息固定下载文件使用方法  
  39.         //return ServletActionContext.getServletContext().getResourceAsStream(inputPath);  
  40.     }  
  41.     public String getDownloadFileName(){  
  42.         //如果是中文的文件名称需要转码  
  43.         String downloadFileName = "";  
  44.         try {  
  45.             downloadFileName = URLEncoder.encode("文件下载.jpg","UTF-8");  
  46.         } catch (UnsupportedEncodingException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.         return downloadFileName;  
  50.     }  
  51. }  
  52. </span>  

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页