struts2附件上传下载管理
jsp代码:
?
<form action="upload.action" method="post" enctype="multipart/form-data"> <table> <tbody> 选择第一个文件:<input type="file" name="upload" /><br> 选择第二个文件:<input type="file" name="upload" /><br> 选择第三个文件:<input type="file" name="upload" /><br> <input value="上传" type="submit" /> </tbody> </table> </form><table cellspacing="1" id="roleTable" style="width:98%" ><thead><tr> <th width="30"><input type="checkbox" onClick="checkallRows(this)" id="checkall"></input></th> <th width="20"></th> <th>文件名称</th> <th>上传人员</th> <th>文件大小</th> <th>上传时间</th> <th></th> <th></th> </tr> </thead> <tbody> <s:iterator value="attachmentList" id="attachment" status="tpls"> <tr > <td><input type="checkbox" name="cName" value="${staffId}"/></td> <td><a href="#" name="code">public List<Attachment> attachmentList;/** * 上传 * @return * @throws Exception */ public String upload() throws Exception { //getUpload()是临时文件 List<File> files = getUpload(); String fileName = ""; for (int i = 0 ; i < files.size() ; i++) { //fileName= "c3p0-0.9.1.2.bin.zip" fileName = getUploadFileName().get(i); Attachment a = new Attachment(); a.setFileName(fileName); String filetype = fileName.substring(fileName.lastIndexOf(".")); //fileurl= "20101228181435906.zip" String fileurl = DateCl.getDateToFilename() + filetype; a.setCreateTime(DateCl.getFullDayTime()); //D:\Tomcat 6.0\webapps\bip-platform\\upload\yyyy-MM-dd File path = new File(getSavePath() + "\" + DateCl.getDay()); boolean bFile = path.exists(); if( bFile == false ){ bFile = path.mkdir(); } a.setFilePath(path.getPath() + "\" + fileurl); FileOutputStream fos = new FileOutputStream(path.getPath() + "\" + fileurl); FileInputStream fis = new FileInputStream(files.get(i)); byte[] buffer = new byte[1024]; int len = 0; long totalLen = 0; while ((len = fis.read(buffer)) > 0) { totalLen += len; fos.write(buffer , 0 , len); } a.setFileSize(totalLen); jbpmAttachmentService.saveAttachmentPO(a); } return SUCCESS; } /** * 下载 * @return * @throws Exception */ public String download()throws Exception{ HttpServletRequest request = ServletActionContext.getRequest();downloadFilePath = (String)request.getParameter("downloadFilePath"); downloadFileName = (String)request.getParameter("downloadFileName"); downloadFilePath = fileUploadUtil.convertDownloadFilePath(getSavePath(), downloadFilePath); convertDownloadFilePath(); return SUCCESS; } /** * 组织下载文件路径 * @throws Exception * */ private void convertDownloadFilePath() throws Exception{ //得到这个路径upload\20101221190611093.zip D:\Tomcat 6.0\webapps\bip-platform\\upload String importPath = getSavePath(); importPath = importPath.substring(importPath.lastIndexOf("\")+1, importPath.length()); downloadFilePath = downloadFilePath.substring(downloadFilePath.lastIndexOf(importPath), downloadFilePath.length()); } public InputStream getTargetFile() throws Exception {InputStream is = ServletActionContext.getServletContext().getResourceAsStream(downloadFilePath);return is;} /** * 展示 * @return */ public String showUpload(){ attachmentList = jbpmAttachmentService.getAllAttachment(); return SUCCESS; } /** * 删除 * @param upload */ public String deleteUpload(){// File deleteFile = new File(downloadFilePath);// if(deleteFile != null){// deleteFile.delete();// } HttpServletRequest request = ServletActionContext.getRequest();String id = (String)request.getParameter("id");jbpmAttachmentService.deleteAttachmentPO(Long.parseLong(id)); return SUCCESS; }?struts_upload.xml文件代码:
?
<constant name="struts.multipart.maxSize" value="104857601"></constant><constant name="struts.custom.i18n.resources" value="messageResource"/><!-- 附件管理--><package name="upload" namespace="/uploadMgr"extends="struts-default"><!-- 显示上传页面 --><action name="showUpload" type="chain">showUpload</result><result name="input" >showUpload.jsp</result></action> <!-- 下载 --><action name="download" type="stream"> <param name="contentType">application/zip</param> <param name="inputName">targetFile</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">4096</param> </result> </action> <!-- 删除附件 --><action name="deleteUpload" type="chain">showUpload</result></action>