struts2 多文件上传与下载

来源:互联网 发布:矩阵论教程 张绍飞pdf 编辑:程序博客网 时间:2024/06/03 23:38
 List<File> upload;//上传文件名集合   private List<String>  uploadFileName;//上传的文件的自带名有后缀private InputStream inputStream; // 下载用的List<Fileform> addlist4;// 接收项目文件public List<String> getUploadFileName() {return uploadFileName;}public void setUploadFileName(List<String> uploadFileName) {this.uploadFileName = uploadFileName;}public List<File> getUpload() {return upload;}public void setUpload(List<File> upload) {this.upload = upload;}public InputStream getInputStream() {return inputStream;}public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}文件上传的方法:if (addlist4!=null) {int j=0;for (int i = 0; i < addlist4.size(); i++) {Fileform file = addlist4.get(i);if(file!=null){String path = saveUploadFile(upload.get(j++));String filename=this.getUploadFileName().get(i);file.setFilename(filename);file.setPath(path);file.setOtherformid(project.getProjectid());file.setFileforename(file.getFileforename());file.setDeletestate(0);fileformService.save(file);}}}/** * 保存上传的文件,并返回文件在服务端的真实存储路径 *  * @param upload * @return */protected String saveUploadFile(File upload) {SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");// >> 获取路径String basePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload_files");String subPath = sdf.format(new Date());// >> 如果文件夹不存在,就创建File dir = new File(basePath + subPath);if (!dir.exists()) {dir.mkdirs(); // 递归的创建不存在的文件夹}// >> 拼接路径String path = basePath + subPath + UUID.randomUUID().toString();// >> 移动文件upload.renameTo(new File(path)); // 如果目标文件夹不存在,或是目标文件已存在,就会不成功,返回false,但不报错。return path;}下载的方法:/** 下载 */public String download() throws Exception {// 准备下载的资源Fileform fileform = fileformService.getById(model.getFileid());inputStream = new FileInputStream(fileform.getPath());// 准备文件名(解决乱码问题)String fileName = URLEncoder.encode(fileform.getFilename(), "utf-8"); // 方法一ActionContext.getContext().put("fileName", fileName);return "download";}jsp:页面:<script type="text/javascript">var num4=0;function addfile(){    var tr = document.createElement('tr');var td = document.createElement('td');         td.innerHTML = "<td ><input type='text' name='addlist4["+num4+"].fileforename' required='required' id='filename'></td>"; tr.appendChild(td); td = document.createElement('td'); td.innerHTML = "<td > <input type='file' name='upload'  value='浏览'  style='width:200px;'></td>"; tr.appendChild(td); td = document.createElement('td'); td.innerHTML = "<td  ><textarea rows='2' cols='20' required='required' name='addlist4["+num4+"].filedesc'></textarea></td>"; tr.appendChild(td); td = document.createElement('td'); td.innerHTML = "<td> <input type='button' value='删除' onclick='removeSelect4(this)'/></td>"; tr.appendChild(td); var dct = document.getElementById("table4"); dct.tBodies[0].appendChild(tr); ++num4;            }function removeSelect4(inputobj){     var parentTD = inputobj.parentNode;       var parentTR = parentTD.parentNode;       var parentTBODY = parentTR.parentNode;         parentTBODY.removeChild(parentTR);}</script>struts2的配置:在特定的action中<!-- 下载专用的结果配置 --><result name="download" type="stream"><param name="contentType">application/octet-stream</param><param name="inputName">inputStream</param><param name="contentDisposition">attachment;filename="${#fileName}.doc"</param></result>

0 0
原创粉丝点击