博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java附件上传下载磁盘版
阅读量:5233 次
发布时间:2019-06-14

本文共 6684 字,大约阅读时间需要 22 分钟。

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) {            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( "
"); } 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(Map
fileInfo) { 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; }}

DAO

@Servicepublic class UploadService {    @Autowired    UploadDao uploaddao;    // 保存    public int save(Map
fileInfo) { 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; }}

 

转载于:https://www.cnblogs.com/fxfly/p/4784795.html

你可能感兴趣的文章
Facebook----扎克伯格
查看>>
mac下破解apk文件以及apktool的相关使用
查看>>
优化网站设计(二十六):设计“智能”的事件处理程序
查看>>
性能测试总结(一)---基础理论篇
查看>>
前端程序员容易忽视的一些基础知识
查看>>
ISO日期格式标准,浏览器到服务器到mysql中的时区
查看>>
python 函数、装饰器、迭代器、生成器、列表生成式
查看>>
捕获JSON 解析错误
查看>>
java-类和重载笔记
查看>>
java中Date,SimpleDateFormat
查看>>
Swift学习Day005
查看>>
第九章笔记
查看>>
12558 - Egyptian Fractions (HARD version) (IDA* + 剪枝)
查看>>
Wireshark 文件分割和合并
查看>>
结对编程项目-四则运算 挑战出题
查看>>
数组是什么
查看>>
【MM自动记账】自动记账事务说明(转)
查看>>
MyBatis_SelectKey使用oracle 序列插入主键
查看>>
System.getproperty()能取到的值
查看>>
插值法(内插法)
查看>>