[jsp]代码库
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileUpload" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletRequestContext" %>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="org.apache.commons.fileupload.RequestContext" %>
<%@ page import="java.util.*" %>
<% String workItemID="";
String affixnameTmp = "";
String affixnameIdTmp = "";
Date date = new Date();
RequestContext requestContext = new ServletRequestContext(request);
////检查是否是一个文件上传请求 FileUpload.isMultipartContent(requestContext)
if (FileUpload.isMultipartContent(requestContext)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("C://sunyard//sunflow230//Tomcat4//webapps//webench//tmp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("gbk");
upload.setSizeMax(10000000); //10M
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
System.out.println("文件上传发生错误" + e1.getMessage());
}
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem fileItem = (FileItem) it.next();
if(fileItem.getFieldName().equalsIgnoreCase("workItemID")){
workItemID=new String(fileItem.getString().getBytes("iso8859-1"),"gbk");
}
if(fileItem.getFieldName().equalsIgnoreCase("affixname")){
affixnameTmp=new String(fileItem.getString().getBytes("iso8859-1"),"gbk");
}
if(fileItem.getFieldName().equalsIgnoreCase("affixnameId")){
affixnameIdTmp=new String(fileItem.getString().getBytes("iso8859-1"),"gbk");
}
if (fileItem.isFormField()) {
} else {
if (fileItem.getName() != null && fileItem.getSize() != 0) {
File fullFile = new File(fileItem.getName());
String affixname="",affixnameId="";
affixname = fullFile.getName();
int n = affixname.indexOf(".");
affixnameId = String.valueOf(date.getTime())+affixname.substring(n);
File newFile = new File("C:/sunyard/sunflow230/Tomcat4/webapps/webench/upload/" + affixnameId);
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
affixnameTmp=affixnameTmp+affixname+";";
affixnameIdTmp=affixnameIdTmp+affixnameId+";";
} else {
System.out.println("文件没有选择 或 文件内容为空");
}
}
}
}
// response.sendRedirect("upload.jsp?&workItemID="+workItemID+"&affixnameId=" + affixnameIdTmp + "&affixname=" + affixnameTmp);
out.println("<script>window.location='upload.jsp?&workItemID="+workItemID+"&affixnameId=" + affixnameIdTmp + "&affixname=" + affixnameTmp + "'</script>");
%>