public class UploadAction extendsBaseAction {private static final long serialVersionUID = 1L;
@AutowiredprivateUploadService uploadser;publicString index() {return "upload";
}//附件上传
public void up() throwsIOException {//设置文件缓存地址和缓存大小,需要时在设置/
File bufferTemp = newFile(Path.UPLOAD_FILES);if (!bufferTemp.exists()) {
bufferTemp.mkdirs();
}if (attfile != null) {
Map fileInfo =getfileInfo();
File savefile= new File(bufferTemp, fileInfo.get("fileid") + "."
+ fileInfo.get("fsuffix"));if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(attfile, savefile);
response.setContentType("text/html;charset=UTF-8");if (uploadser.save(fileInfo) > 0) {
response.getWriter().write("
+ fileInfo.get("fileid") + "', '"
+ fileInfo.get("filename")+ fileInfo.get("fsuffix") + "', 'success', '"
+ fileInfo.get("filetype") + "');");
}else{
response.getWriter().write("
+ fileInfo.get("fileid") + "', '"
+ fileInfo.get("filename")+ fileInfo.get("fsuffix") + "', 'failure', '"
+ fileInfo.get("filetype") + "');");
}
}
}//附件下载
public void down() throwsIOException {
Map fileinfo = new HashMap();
fileinfo=uploadser.getFileInfo(fileid);
String filename= fileid + "." + fileinfo.get("fsuffix");
File file= new File(Path.UPLOAD_FILES + "\\" +filename);if (!file.exists()) {
response.sendError(404, "File not found!");
}
BufferedInputStream br= new BufferedInputStream(newFileInputStream(
file));byte[] buf = new byte[1024];int len = 0;
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="
+ URLEncoder.encode(fileinfo.get("filename") + "."
+ fileinfo.get("fsuffix"), "utf-8"));
OutputStream out=response.getOutputStream();while ((len = br.read(buf)) > 0)
out.write(buf,0, len);
br.close();
out.close();
}//删除单个附件
public void del() throwsIOException {int row =uploadser.delByFileId(fileid);
response.getWriter().print(row> 0 ? true : false);
}//组合上传附件信息
private MapgetfileInfo() {
Map infoMap = new HashMap();
infoMap.put("fileid", Tool.getUUID());
infoMap.put("filename",
attfileFileName.substring(0, attfileFileName.lastIndexOf(".")));
infoMap.put("filetype", filetype);
infoMap.put("dataid", dataid);
infoMap.put("fcreateuserid", getLogin().getId());
infoMap.put("fcreateusername", getLogin().getName());
infoMap.put("fcreatetime",
DateUtil.getCurrentDateAsString("yyyy-MM-dd"));
infoMap.put("fsuffix", attfileFileName.substring(
attfileFileName.lastIndexOf(".") + 1, attfileFileName.length()));returninfoMap;
}privateString postfix;privateString filetype;privateString dataid;privateString fileid;privateFile attfile;privateString attfileFileName;publicString getFileid() {returnfileid;
}public voidsetFileid(String fileid) {this.fileid =fileid;
}publicString getPostfix() {returnpostfix;
}public voidsetPostfix(String postfix) {this.postfix =postfix;
}publicString getFiletype() {returnfiletype;
}public voidsetFiletype(String filetype) {this.filetype =filetype;
}publicString getDataid() {returndataid;
}public voidsetDataid(String dataid) {this.dataid =dataid;
}publicFile getAttfile() {returnattfile;
}public voidsetAttfile(File attfile) {this.attfile =attfile;
}publicString getAttfileFileName() {returnattfileFileName;
}public voidsetAttfileFileName(String attfileFileName) {this.attfileFileName =attfileFileName;
}
}