Struts文件上传下载

来源:互联网 发布:怎么用淘宝格子铺 编辑:程序博客网 时间:2024/05/16 08:29

一.Struts文件上传:

  添加相关jar包

1.UploadAction:

package com.lzh.lms.web.struts.action.file;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.lzh.lms.util.StringUtil;import com.opensymphony.xwork2.ActionSupport;/** * 档案上传 *  * @author luozehua * @date 2014年3月20日 * @time 下午3:45:36 */@SuppressWarnings("serial")public class UploadAction extends ActionSupport {private File file;private String fileFileName;private String fileContentType;private String allowedTypes;public String getAllowedTypes() {return allowedTypes;}public void setAllowedTypes(String allowedTypes) {this.allowedTypes = allowedTypes;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}public String getFileContentType() {return fileContentType;}public void setFileContentType(String fileContentType) {this.fileContentType = fileContentType;}@Overridepublic String execute() throws Exception {String uploadPath = ServletActionContext.getServletContext().getRealPath("/files");File file = new File(uploadPath);if (!file.exists()) {file.mkdirs();}// 上传文件后,文件名后面加上当前世间,避免重复导致覆盖文件Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");String newPrefix = StringUtil.getStringExceptFileSuffix(this.fileFileName).concat(format.format(date));String suffix = StringUtil.getFileSuffix(this.fileFileName);String newFileName = newPrefix.concat(".").concat(suffix);FileUtils.copyFile(this.file, new File(file, newFileName));return "upload_success";}}
2.jsp

<h2>上传你的档案</h2><form action="${pageContext.request.contextPath }/file/upload.action" method="post" enctype="multipart/form-data">文件:<input type="file" name="file"><font color="red" size="2">*上传文件不得超过10M,格式必须是我doc或docx!</font><br><input type="submit" value="上传"></form>

3.struts配置文件

<constant name="struts.multipart.saveDir" value="d:/temp" /><constant name="struts.multipart.maxSize" value="6291456" /><package name="file" extends="struts-default" namespace="/file"><action name="upload" class="uploadAction"><interceptor-ref name="fileUpload"><param name="allowedTypes">application/vnd.ms-doc,application/octet-stream</param></interceptor-ref><interceptor-ref name="defaultStack"/><result name="upload_success">/jsp/file/upload_success.jsp</result><result name="input">/jsp/file/upload_failure.jsp</result></action></package>

一.struts 文件下载

1.DownloadAction

package com.lzh.lms.web.struts.action.file;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/** * @author luozehua * @date 2014年3月23日 * @time 下午8:44:53 */@SuppressWarnings("serial")public class DownloadAction extends ActionSupport {private String fileName;public String getFileName() throws UnsupportedEncodingException {this.fileName = new String(this.fileName.getBytes(), "ISO-8859-1");return fileName;}public void setFileName() throws UnsupportedEncodingException {String name = ServletActionContext.getRequest().getParameter("fileName");name = new String(name.getBytes("iso-8859-1"), "UTF-8");this.fileName = name;}public InputStream getInputStream() throws UnsupportedEncodingException {FileInputStream in = null;this.setFileName();String filePath = ServletActionContext.getServletContext().getRealPath("/download");try {String result = URLDecoder.decode(this.fileName, "GBK");in = new FileInputStream(new File(filePath, result));} catch (FileNotFoundException e) {e.printStackTrace();}return in;}@Overridepublic String execute() throws Exception {return super.execute();}}

2.jsp

<h2>下载档案模版</h2><a href="${pageContext.request.contextPath }/file/download.action?fileName=个人档案模板.doc">档案模版</a><hr />

3.struts 配置文件

<package name="download" extends="struts-default" namespace="/file"><action name="download" class="downloadAction"><result type="stream"><param name="inputName">inputStream</param><param name="contentDisposition">attachment;filename=${fileName}</param></result></action></package>


0 0