首页 > 其他分享 >strtus2文件上传与下载

strtus2文件上传与下载

时间:2022-10-14 11:07:52浏览次数:38  
标签:return String private File strtus2 import 上传 public 下载


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 使必须一致]

标签:return,String,private,File,strtus2,import,上传,public,下载
From: https://blog.51cto.com/u_12952267/5755722

相关文章

  • 阿里云服务器下载速度
    网络运营商给的带宽定义是1M=1Mbps(megabitspersecond兆比特每秒),单位是bit(比特),但是下载速度的单位是byte(字节)1byte=8bit所以实际的下载网速1Mbps=10......
  • 通过iframe下载文件
    //iframe下载文件functiondownloadFileByIfr(downLoadUrl){//移除旧的节点constoldNode=document.querySelector("#g-exportOrder-iframe")if(oldNode)......
  • flask上传、下载文件
    fromflaskimportFlask,render_template,request,send_filefromwerkzeug.utilsimportsecure_filename#创建appapp=Flask(__name__)#上传文件-网页端html_cod......
  • maven下载、安装,创建
    1,maven准备:需要安装JDK并配置好环境变量下载地址:https://maven.apache.org/download.cgi2,下载完成之后解压到当前文件夹:D:\apache-maven-3.8.6 3,配置Maven环境变量......
  • Java 下载excel模板下载之后无法正常打开,提示损坏
    简单版:resources目录下放模板excel文件,通过接口下载后,有文件,但打不开。解决:maven构建时对该excel模板进行了过滤,导致文件损坏,解决办法,在过滤的时候把xlsx排除掉(......
  • ctfshow文件上传总结
    简单总结下 文件上传前端限制绕过:抓包修改对应项即可,不过本人更习惯上传一个符合要求的文件,然后抓包后只修改后缀名,看看能不能上传成功文件上传后端限制绕过:一般都是检......
  • .Net FTP 自动创建目录/上传文件
    //上传文件publicstaticBooleanFtpUpload(stringftpPath,stringlocalFile){//检查目录是否存在,不存在创建FtpCheckDirectoryExist(ftpPath);......
  • 陆地观测卫星数据服务(CRESDA)订单ftp地址错误—已解决不能下载问题
    陆地观测卫星数据服务订单ftp地址错误问题:本人在陆地观测卫星数据网站上申请GF1-WFV10幅数据,订单完成后返回的FTP地址出现无法连接服务器现象。(数据订单申请已通过)一、情......
  • 【镜像下载】Windows 11 Build 22000.160 八合一纯净版
    今天凌晨,微软向Windows预览体验计划用户推送了最新的Windows11 系统更新,推送补丁为KB5005189,版本号为22000.160,本次推送面向Beta和Dev渠道。本次更新没有带来大......
  • 【镜像下载】Windows 11 Build 22000.168 八合一纯净版
    今天凌晨,微软向Windows预览体验计划用户推送了最新的Windows11 系统更新,推送补丁为KB5005191,版本号为22000.168,本次推送面向Beta和Dev渠道。本次更新没有带来大......