<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <script type="text/javascript"> function AddMore(){ var more = document.getElementById("file"); var br = document.createElement("br"); var input = document.createElement("input"); var button = document.createElement("input"); input.type = "file"; input.name = "file"; button.type = "button"; button.value = "删除"; more.appendChild(br); more.appendChild(input); more.appendChild(button); button.onclick = function(){ more.removeChild(br); more.removeChild(input); more.removeChild(button); }; } </script> </head> <body> <s:form action="upload" method="post" theme="simple" enctype="multipart/form-data"> <table border="1" width="50%"> <tr> <td>用户名:</td> <td><s:textfield name="username" label="用户名"></s:textfield></td> </tr> <tr> <td>附件:</td> <td id="file"> <s:file name="file" label="文件"></s:file> <input type="button" value="增加附件" onclick="AddMore()"> </td> </tr> <tr> <td colspan="2" align="center"> <s:submit value="提交" ></s:submit></td> </tr> </table> </s:form> </body> </html>
编辑页面遍历显示 :
<c:forEach var="i" items="${oaAnnexs}" > <a href="<%=url%>${i.filePath }">${i.fileName}</a><input type="hidden" value="${i.id }"/> <input type="button" value="删除" onclick="delFile('${i.id }')"/> <br> </c:forEach>
底层代码:
package com.sinosoft.oa.base.service.impl; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * * @author bmj * @date 2014-4-30 */ public class FileOperateUtil { private static final String REALNAME = "realName"; private static final String STORENAME = "storeName"; private static final String SIZE = "size"; private static final String SUFFIX = "suffix"; private static final String CONTENTTYPE = "contentType"; private static final String CREATETIME = "createTime"; private static final String UPLOADDIR = "uploadDir/"; /** * 将上传的文件进行重命名 * * @author lds * @date 2014-4-30 * @param name * @return */ private static String rename(String name) { Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss") .format(new Date())); Long random = (long) (Math.random() * now); String fileName = now + "" + random; if (name.indexOf(".") != -1) { fileName += name.substring(name.lastIndexOf(".")); } return fileName; } /** * 上传文件 * * @author lds * @date 2014-4-30 * @param request * @param params * @param values * @return * @throws Exception */ public static List<Map<String, Object>> upload(HttpServletRequest request, String[] params, Map<String, Object[]> values) throws Exception { List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = mRequest.getFileMap(); String uploadDir = request.getSession().getServletContext() .getRealPath("/") + FileOperateUtil.UPLOADDIR; File file = new File(uploadDir); if (!file.exists()) { file.mkdir(); } String fileName = null; int i = 0; for (Iterator<Map.Entry<String, MultipartFile>> it = fileMap.entrySet() .iterator(); it.hasNext(); i++) { Map.Entry<String, MultipartFile> entry = it.next(); MultipartFile mFile = entry.getValue(); fileName = mFile.getOriginalFilename(); String storeName = rename(fileName); //fileName去掉后缀 if(!StringUtils.isBlank(fileName)){ fileName = fileName.substring(0,fileName.lastIndexOf(".")); } File file2 = new File(uploadDir+storeName); //上传 FileCopyUtils.copy(mFile.getBytes(), file2); Map<String, Object> map = new HashMap<String, Object>(); map.put(mFile.getOriginalFilename(), storeName); result.add(map); } return result; } /** * 下载 * * @author lds * @date 2014-4-30 * @param request * @param response * @param storeName * @param contentType * @param realName * @throws Exception */ public static void download(HttpServletRequest request, HttpServletResponse response, String storeName, String contentType, String realName) throws Exception { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); BufferedInputStream bis = null; BufferedOutputStream bos = null; String ctxPath = request.getSession().getServletContext() .getRealPath("/") + FileOperateUtil.UPLOADDIR; String downLoadPath = ctxPath + storeName; long fileLength = new File(downLoadPath).length(); response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); } }
相关推荐
jquery实现单附件,多附件上传demo
JQuery多附件上传插件,能够实现多个附件同时上传,格式包括各种文档,视频,图片不限。
自己些的多附件上传下载的C#代码,初学者很适合的。aaaaaaaaaaaaaaaa
一款基于JavaScript实现的多附件上传表单添加类库源码及例子程序
多附件上传效果js多附件上传效果js多附件上传效果js
协同办公软件Domino平台中多附件上传实例
支持多附件上传, 删除记录信息 自动删除文件信息,避免服务器垃圾文件。
利用ajax技术一次可以上传多个附件MultiUpload的Demo。
支持多文件上传、单个/批量附件下载,代码注释详细
实现多附件的上传,可以在多个上传框中添加附件
Lotus domino BS 多附件上传例子 Lotus domino jquery附件上传 经典完整例子,全部源码,使用在多个项目中,请放心下载。
// 多附件上传 MultipartRequestHandler mrh = conForm.getMultipartRequestHandler(); Map formFiles = mrh.getFileElements(); String fileName=""; if (formFiles!=null){ AnnexsUpload au = new ...
多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery多附件上传组件、jquery
ASP.NET多附件上传和附件编辑的实现 ASP.NET多附件上传和附件编辑的实现 ASP.NET多附件上传和附件编辑的实现 ASP.NET多附件上传和附件编辑的实现
1、可以一次性选择多个文件。 2、有上传进度条,可以自行设置。 3、方便灵活。
多附件上传 代码
asp.net多附件上传实例代码,里面包括多种实现的方法
简单的多附件上传案例.与单附件上传类似.开发中遇到了.weblogic环境下页面发送ajax后台接收不到文件等问题都有说明