strtus2文件上传与下载

xiaoxiao2021-02-28  8

10820171017 package com.tiger.upload; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.URLEncoder; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 文件下载Action * 此action返回一个InputStream对象 * 自定义一个方法(下载文件入口输入流,必须将此方法名注入到InputName属性中) * @author tiger * @time 2017年10月17日 */ public class FileDownLoadAction extends ActionSupport{ private String downPath; //下载路径 private String filename; //下载文件名 private String preFilename = "eeeeeee.txt"; @Override public String execute() throws Exception { //设置编码格式 setFilename(URLEncoder.encode(preFilename,"UTF-8")); return SUCCESS; } /** * 下载文件的入口方法 * @return */ public InputStream getTargetFile() { FileInputStream fis = null; try { fis = new FileInputStream(getDownPath()+File.separator+preFilename); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("下载成功,下载路径 = " + getDownPath()); return fis; // return ServletActionContext.getServletContext().getResourceAsStream(getDownPath()+preFilename); } public String getDownPath() { return ServletActionContext.getServletContext().getRealPath("/WEB-INF/" + downPath); } public void setDownPath(String downPath) { this.downPath = downPath; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } } package com.tiger.upload; import java.io.*; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 文件上传action * @author tiger * @time 2017年10月17日 */ public class FileUploadAction extends ActionSupport{ private static final long serialVersionUID = 1L; private File upload; //文件对象(名字必须这样) private String uploadContentType; //文件类型(名字必须这样) private String uploadFileName; //文件名(名字必须这样) private String title; //文件标题 private String upPath; //文件上传保存路径 @Override public String execute() throws Exception { FileInputStream fis = new FileInputStream(getUpload()); FileOutputStream fos = new FileOutputStream(getUploadFolder()+File.separator+getUploadFileName()); int len = 0; byte[] buff = new byte[1024]; while ((len = fis.read(buff)) != -1) { fos.write(buff, 0, len); } fos.flush(); fos.close(); fis.close(); System.out.println("上传成功"); return SUCCESS; } /** * 获取文件上传路径 * @return */ private String getUploadFolder() { String uploadFolder = getUpPath(); File file = new File(uploadFolder); if (!file.exists()) { file.mkdirs(); } return uploadFolder; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUpPath() { return ServletActionContext.getServletContext().getRealPath("/WEB-INF/"+upPath); } public void setUpPath(String upPath) { this.upPath = upPath; } } <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> 上传 & 下载
文件标题:
选择文件:
上传:
200000 fileSavePath /WEB-INF/content/jsp/success.jsp /file.jsp fileSavePath targetFile attachment;filename=${filename} 200000 10820171017 Struts2文件上传 & 下载 1、文件上传 1)、加载 commons-fileupload.jar(默认)[另外还有cos、pell ,配置struts.multipart.parser=jakarta] 2)、表单提交数据类型必须是 multipart/form-data ,提交方式必须是 post 3)、编写Action类[下边三个属性名必须是那样] File upload; //上传文件对象 String uploadContentType; //获取上传文件的类型 String uploadFileName; //获取文件名 2、文件下载 1)、编写下载文件输入流的入口方法 getTargetFile();[此方法在strtus.xml中配置 inputName 使必须一致]
转载请注明原文地址: https://www.6miu.com/read-1442634.html

最新回复(0)