文件上传下载
来源:互联网 发布:中讯软件集团 编辑:程序博客网 时间:2024/06/07 23:27
文件上传action
private List<File> file;//文件列表private List<String> fileFileName;//文件名称private List<String> fileContentType;//文件类型@Action(value = "saveAttaFile")public void saveAttaFile(){ String address = (String) context.getAttribute("File_UPLOAD_URL");//可配置的文件路径 //以日期年月为文件夹保存文件 Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH )+1; String filename = year+"-"+month; String path = address + "\\\\file\\\\" + filename; String uuid=""; if(file != null){ for(int i=0;i<file.size();i++){ try { String attaName = mesAttachmentService.saveAttachmentFile(path, file.get(i), fileFileName.get(i)); } catch (Exception e) { e.printStackTrace(); } } } this.printText(uuid); }
文件上传方法
public String saveAttachmentFile(String path, File file, String fileName) throws Exception { // TODO Auto-generated method stub if(file!=null){ InputStream in = new FileInputStream(file); String name = new Date().getTime()+""+Math.round(Math.random()*100);//数据库中保存的文件名称 File folder = new File(path); if (!folder.exists()) { folder.mkdirs(); } String str = fileName.substring(fileName.lastIndexOf(".")); String attaName = name+str;//通过时间戳组文件名 File uploadFile = new File(path,attaName); OutputStream out = new FileOutputStream(uploadFile); byte[] buffer = new byte[1024 * 1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); return attaName; } return null;}
下载文件的方法
//数据库有专门表保存文件信息,前台通过传递文件id获取详细信息,并下载文件。public void downMesAttaFile(){ String address = (String) context.getAttribute("File_UPLOAD_URL");//可配置的路径 String attaId = request.getParameter("attaid");//文件id MesAttachment mesAtta;//文件实体类 try { mesAtta = this.ptBaseService.get(MesAttachment.class, attaId); if(StringUtil.isNotBlank(mesAtta)){ HttpServletResponse response = ServletActionContext.getResponse(); ServletContext context = ServletActionContext.getServletContext(); String agent = request.getHeader("User-Agent").toUpperCase(); String downName = ""; //部分浏览器下载文件弹窗文件名乱码,进行编码 if (agent.indexOf("MSIE") > 0|| ( null != agent && -1 != agent.indexOf(") LIKE GECKO"))) { downName = URLEncoder.encode(mesAtta.getAttaName(), "UTF-8"); } else { downName = new String(mesAtta.getAttaName().getBytes("UTF-8"), "ISO8859-1"); } // + URLEncoder.encode(mesAtta.getAttaName(), "utf-8") response.setHeader("content-disposition", "attachment;filename=" + downName); String realPath = context.getRealPath("/WEB-INF/download"); InputStream is = new FileInputStream(address + mesAtta.getAttaUrl()); OutputStream os = response.getOutputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = is.read(b)) > 0) { os.write(b, 0, len); } is.close(); os.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }}
前台页面下载
/**前端下载文件,非ajax无刷新页面下载,可以在弹窗片段页面使用*/function downFile(id){ if (typeof (downFile.iframe) == "undefined") { var iframe = document.createElement("iframe"); downFile.iframe = iframe; document.body.appendChild(downFile.iframe); } downFile.iframe.src = "action"+id; downFile.iframe.style.display = "none"; }
阅读全文
0 0
- 文件上传下载
- 文件上传下载
- 上传下载文件
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 上传下载文件
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 上传下载文件 .
- 上传下载文件
- 文件上传下载
- 文件上传下载
- 文件上传下载
- 【数据库分区】SQL Server表分区方法
- Python Web开发环境搭建
- 信号屏蔽pending
- 手把手教你用Vue2+webpack+node开发一个H5 app
- 插入排序(直接插入排序,希尔排序)
- 文件上传下载
- CentOS6图形化安装
- sap Status状态栏设计
- Liunx16.04安装Apache-tomcat
- unity3d优化总结
- numpy、xlrd和xlwt库的安装(windows平台)
- Kotlin 为何能成为 Android 官方支持开发语言及Kotlin开发Android应用初体验
- 将DataTable转换为Json
- 软件性能测试面试题二