📄 fileupaction.java
字号:
package com.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.apache.struts2.ServletActionContext;
import com.model.TFile;
import com.opensymphony.xwork2.ActionSupport;
import com.service.FileService;
public class FileUpAction extends ActionSupport {
/**
* 文件上传以及列表显示,删除操作
*/
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024;
protected FileService fileService;
List fileList;
private int id;
//要上传的文件
private File[] upload;
//由struts2获得的文件名
private String[] uploadFileName;
//由struts2获得的文件类型
private String[] uploadContentType;
//保存路径
private String filePath;
//文件备注
private String[] fileRemark;
//上传者
private String[] fileAuthor;
//从数据库读出的文件名,注意和uploadFileName的区别
private String fileName;
//上传时间
private Date updateTime;
public void setFileService(FileService fileService) {
this.fileService = fileService;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String[] getFileRemark() {
return fileRemark;
}
public void setFileRemark(String[] fileRemark) {
this.fileRemark = fileRemark;
}
public String[] getFileAuthor() {
return fileAuthor;
}
public void setFileAuthor(String[] fileAuthor) {
this.fileAuthor = fileAuthor;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public List getFileList() {
return fileList;
}
public void setFileList(List fileList) {
this.fileList = fileList;
}
/**
* 复制文件方法
*/
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 上传多个文件action
*/
public String upload(){
for (int i = 0; i < this.getUpload().length; i++) {
String dstPath=ServletActionContext.getServletContext()
.getRealPath(this.getFilePath()) + "\\";
String targetFileName = generateFileName(uploadFileName[i]);
File dstFile = new File(dstPath,targetFileName);
copy(this.getUpload()[i], dstFile);
String targetPath=dstPath+targetFileName;
fileService.save(uploadFileName[i],targetFileName, targetPath, fileRemark[i], fileAuthor[i]);
}
return SUCCESS;
}
/**
* 列出所有文件
*/
public String getAllFiles(){
this.fileList=fileService.getAllFile();
return SUCCESS;
}
/**
* 根据ID删除文件和数据库中文件信息
*/
public String delete(){
TFile tfile=fileService.findFileById(id);
File delFile=new File(tfile.getFilePath());
delFile.delete();
fileService.deleteById(id);
return SUCCESS;
}
/**
* 获得上传文件总数
*/
public int getCount(){
int count=fileService.getFilesCount();
return count;
}
/**
* 随机生成重命名文件名字
*/
public String generateFileName(String fileName)
{
String formatDate = new SimpleDateFormat("yyMMddHHmmss")
.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return formatDate + random + extension;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -