<!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();
}
}