Struts2实现上传下载
上传jsp页面代码:
- <span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP '03.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <link rel="stylesheet" type="text/css" href="css/common.css" />
- <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
- <script type="text/javascript">
- $(function(){
- $(".thumbs a").click(function(){
- var largePath=$(this).attr("href");
- var largeAlt=$(this).attr("title");
- $("#largeImg").attr({
- src:largePath,
- alt:largeAlt
- });
- return false;
- });
- });
- </script>
- </head>
- <body>
- <h2> 文件上传</h2>
- <form action="upload.action" method="post" enctype="multipart/form-data">
- 上传文件1:<input type="file" name="upload"/><br>
- 上传文件2:<input type="file" name="upload"/><br>
- 上传文件3:<input type="file" name="upload"/><br>
- <input type="submit" value="提交"> ${result}
- </form>
- <hr>
- <h2>图片预览</h2>
- <p><img id="largeImg" src="images/img1-lg.jpg" alt="Large Image"/></p>
- <p class="thumbs">
- <a href="images/img2-lg.jpg" title="Image2"><img src="images/img2-thumb.jpg"></a>
- <a href="images/img3-lg.jpg" title="Image3"><img src="images/img3-thumb.jpg"></a>
- <a href="images/img4-lg.jpg" title="Image4"><img src="images/img4-thumb.jpg"></a>
- <a href="images/img5-lg.jpg" title="Image5"><img src="images/img5-thumb.jpg"></a>
- <a href="images/img6-lg.jpg" title="Image6"><img src="images/img6-thumb.jpg"></a>
- </p>
- </body>
- </html></span>
- <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
- <constant name="struts.devMode" value="true" />
- <!-- 国际化 -->
- <constant name="struts.custom.i18n.resources" value="app"></constant>
- <package name="default" namespace="/" extends="struts-default">
- <action name="upload" class="com.lihf.action.FileUploadAction">
- <result>/jsp/03.jsp</result>
- <result name="input">/jsp/error.jsp</result>
- <!-- 配置拦截器限制上传文件类型及大小 -->
- <interceptor-ref name="fileUpload">
- <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param>
- <param name="maximumSize">2M</param>
- </interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- </action>
- <action name="download" class="com.lihf.action.DownLoadAction">
- <param name="inputPath">/images/img2-lg.jpg</param>
- <result name="success" type="stream">
- <param name="contentType">application/octet-stream</param>
- <param name="inputName">inputStream</param>
- <!-- 以附件的形式下载 -->
- <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
- <param name="bufferSize">8192</param>
- </result>
- </action>
- </package>
- </struts></span>
- <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>scxz</display-name>
- <servlet>
- <servlet-name>UploadServlet</servlet-name>
- <servlet-class>com.lihf.servlet.UploadServlet</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>DownloadServlet</servlet-name>
- <servlet-class>com.lihf.servlet.DownloadServlet</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>SmartUploadServlet</servlet-name>
- <servlet-class>com.lihf.servlet.SmartUploadServlet</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>SmartDownloadServlet</servlet-name>
- <servlet-class>com.lihf.servlet.SmartDownloadServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UploadServlet</servlet-name>
- <url-pattern>/uploadServlet.do</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>DownloadServlet</servlet-name>
- <url-pattern>/downloadServlet.do</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>SmartUploadServlet</servlet-name>
- <url-pattern>/smartUploadServlet.do</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>SmartDownloadServlet</servlet-name>
- <url-pattern>/smartDownloadServlet.do</url-pattern>
- </servlet-mapping>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app></span>
- <span style="font-size:18px;">package com.lihf.action;
- import java.io.File;
- import java.util.List;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class FileUploadAction extends ActionSupport {
- private List<File> upload;
- private List<String> uploadContentType;
- private List<String> uploadFileName;
- private String result;
- public List<File> getUpload() {
- return upload;
- }
- public void setUpload(List<File> upload) {
- this.upload = upload;
- }
- public List<String> getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(List<String> uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- public List<String> getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(List<String> uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- public String getResult() {
- return result;
- }
- public void setResult(String result) {
- this.result = result;
- }
- @Override
- public String execute() throws Exception {
- String path = ServletActionContext.getServletContext().getRealPath("/images");
- File file = new File(path);
- if(!file.exists()){
- file.mkdir();
- }
- for(int i=0;i<upload.size();i++){
- FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
- }
- result ="上传成功!";
- return SUCCESS;
- }
- }
- </span>
action方法:
- <span style="font-size:18px;">package com.lihf.action;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class DownLoadAction extends ActionSupport {
- public String inputPath;
- public String fileName;
- public String getFileName() {
- return fileName;
- }
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- public String getInputPath() {
- return inputPath;
- }
- public void setInputPath(String inputPath) {
- this.inputPath = inputPath;
- }
- @Override
- public String execute() throws Exception {
- return SUCCESS;
- }
- public InputStream getInputStream() throws IOException{
- String path = ServletActionContext.getServletContext().getRealPath("/images");
- String filePath = path+"\\"+fileName;
- File file = new File(filePath);
- return FileUtils.openInputStream(file);
- //根据文件路径获取流信息固定下载文件使用方法
- //return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
- }
- public String getDownloadFileName(){
- //如果是中文的文件名称需要转码
- String downloadFileName = "";
- try {
- downloadFileName = URLEncoder.encode("文件下载.jpg","UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return downloadFileName;
- }
- }
- </span>