ACTION
public class UploadAction extends BaseAction { private static final long serialVersionUID = 1L; @Autowired private UploadService uploadser; public String index() { return "upload"; } // 附件上传 public void up() throws IOException { // 设置文件缓存地址和缓存大小,需要时在设置/ File bufferTemp = new File(Path.UPLOAD_FILES); if (!bufferTemp.exists()) { bufferTemp.mkdirs(); } if (attfile != null) { MapfileInfo = 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( " "); } else { response.getWriter().write( " "); } } } // 附件下载 public void down() throws IOException { 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(new FileInputStream( 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() throws IOException { int row = uploadser.delByFileId(fileid); response.getWriter().print(row > 0 ? true : false); } // 组合上传附件信息 private Map getfileInfo() { 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())); return infoMap; } private String postfix; private String filetype; private String dataid; private String fileid; private File attfile; private String attfileFileName; public String getFileid() { return fileid; } public void setFileid(String fileid) { this.fileid = fileid; } public String getPostfix() { return postfix; } public void setPostfix(String postfix) { this.postfix = postfix; } public String getFiletype() { return filetype; } public void setFiletype(String filetype) { this.filetype = filetype; } public String getDataid() { return dataid; } public void setDataid(String dataid) { this.dataid = dataid; } public File getAttfile() { return attfile; } public void setAttfile(File attfile) { this.attfile = attfile; } public String getAttfileFileName() { return attfileFileName; } public void setAttfileFileName(String attfileFileName) { this.attfileFileName = attfileFileName; }}
SERVICE
@Servicepublic class UploadService { @Autowired UploadDao uploaddao; // 保存 public int save(MapfileInfo) { return uploaddao.save(fileInfo); } // 取得附件列表 public List
DAO
@Servicepublic class UploadService { @Autowired UploadDao uploaddao; // 保存 public int save(MapfileInfo) { return uploaddao.save(fileInfo); } // 取得附件列表 public List > getFilesByDataId(String dataid) { return uploaddao.getFilesByDataId(dataid); } // 根据附件ID删除 public int delByFileId(String fileid) { Map fileinfo = this.getFileInfo(fileid); int reval = 0; if (fileid.length() > 0) { reval = delDiskFile(fileid + "." + fileinfo.get("fsuffix")) ? uploaddao .delByFileId(fileid) : 0; } return reval; } // 根据业务数据ID批量删除 public int delByDataId(String dataid) { List > filesinfo = this.getFilesByDataId(dataid); Map fileinfo = null; int reval = 0; if (filesinfo.size() > 0) { for (int i = 0; i < filesinfo.size(); i++) { fileinfo = filesinfo.get(i); delDiskFile(fileinfo.get("fileid") + "." + fileinfo.get("fsuffix")); } reval = uploaddao.delByDataId(dataid); } return reval; } // 获取附件信息 public Map getFileInfo(String fileid) { return uploaddao.getFileInfo(fileid); } // 删除磁盘文件 private boolean delDiskFile(String filename) { File file = new File(Path.UPLOAD_FILES + "\\" + filename); boolean suc = false; if (file.exists()) { suc = file.delete(); } return suc; }}