package cn.ynau.lib.action.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = -8920466592471253212L;
private String title;
private File file; // 上传的文件
private String fileFileName; // 文件名称
private String fileContentType; // 文件类型
private String savePath;
private String contents = "";
public String execute() throws Exception {
// String url = getSavePath() + File.separator + getFileFileName();
String url = getSavePath();
System.out.println("url==" + url + ",,," + getFileContentType());
FileOutputStream fos = null;
FileInputStream fis = null;
try {
// fos = new FileOutputStream(url);
File uploadFile = new File(url, this.getFileFileName());
fos = new FileOutputStream(uploadFile);
fis = new FileInputStream(getFile());
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
setContents(getContents() + new String(buffer));
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
return INPUT;
} finally {
close(fos, fis);
}
return SUCCESS;
}
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;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSavePath() {
String result = ServletActionContext.getServletContext().getRealPath(
savePath);
return result;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
private void close(FileOutputStream fos, FileInputStream fis) {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
System.out.println("FileInputStream关闭失败");
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("FileOutputStream关闭失败");
e.printStackTrace();
}
}
}
}
struts2上传下载 解决中文文件名乱码
3星 · 超过75%的资源 需积分: 10 169 浏览量
2013-08-11
15:27:41
上传
评论
收藏 3.31MB RAR 举报
sharzi
- 粉丝: 2
- 资源: 5
最新资源
- 基于keras+fasterRCNN,在VOC格式的口罩数据集上训练,检测人群中有无戴口罩python源码+模型
- 基于opencv+qt5机器视觉的传统缺陷检测, 即采用标准图片和待测图片进行pixel to pixel的XOR操作源码+文档
- 管道内检测缺陷数据库管理系统源码+文档说明+sln
- 毕业设计-低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等)源码+文档说明+截图
- 基于yolov5-tensorRT检测+发动机缸体内壁缺陷检测系统源码+文档说明
- 基于C++实现的锂电池缺陷检测源码+文档说明
- push_version
- 软件自制图像批量压缩工具
- 经典缺陷检测算法源码整理包含PaDiM(2020ICPR)、PatchCore(2022CVPR)、SimpleNet+文档说明
- 基于深度学习的抗梯度噪声的缺陷检测器python源码+文档说明+模型的预训练
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈