<%@ page contentType="text/html; charset=utf-8" language="java"
errorPage=""%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="ip.IPTimeStamp"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<%
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(request.getSession()
.getServletContext().getRealPath("/")
+ "uploadtemp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(3145728);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr());
while (iter.hasNext()) {
FileItem item = iter.next();
String fieldName = item.getFieldName();
if (!item.isFormField()) {
File saveFile = null;
InputStream input = item.getInputStream();
OutputStream output = new FileOutputStream(new File(request
.getSession().getServletContext().getRealPath("/")
+ "upload"
+ File.separator
+ its.getIPTimeRand()
+ "." + item.getName().split("\\.")[1]));
byte data[] = new byte[4096];
int temp = 0;
while ((temp = input.read(data, 0, 4096)) != -1) {
output.write(data);
}
input.close();
output.close();
} else {
String value = item.getString();
%>
普通参数:
<%=value%>
<%
}
}
%>
</body>
</html>
这是JSP的页面
package ip;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Random ;
public class IPTimeStamp {
private SimpleDateFormat sdf = null ;
private String ip = null ;
public IPTimeStamp(){
}
public IPTimeStamp(String ip){
this.ip = ip ;
}
public String getIPTimeRand(){
StringBuffer buf = new StringBuffer() ;
if(this.ip != null){
String s[] = this.ip.split("\\.") ;
for(int i=0;i<s.length;i++){
buf.append(this.addZero(s[i],3)) ;
}
}
buf.append(this.getTimeStamp()) ;
Random r = new Random() ;
for(int i=0;i<3;i++){
buf.append(r.nextInt(10)) ;
}
return buf.toString() ;
}
public String getDate(){
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
return this.sdf.format(new Date()) ;
}
public String getTimeStamp(){
this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;
return this.sdf.format(new Date()) ;
}
private String addZero(String str,int len){
StringBuffer s = new StringBuffer() ;
s.append(str) ;
while(s.length() < len){
s.insert(0,"0") ;
}
return s.toString() ;
}
}
这是IPTimeStamp类
HTTP Status 500 - An exception occurred processing JSP page /fileupload_demo2.jsp at line 31
type Exception report
message An exception occurred processing JSP page /fileupload_demo2.jsp at line 31
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /fileupload_demo2.jsp at line 31
28: if (!item.isFormField()) {
29: File saveFile = null;
30: InputStream input = item.getInputStream();
31: OutputStream output = new FileOutputStream(new File(request
32: .getSession().getServletContext().getRealPath("/")
33: + "upload"
34: + File.separator
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.io.FileNotFoundException: F:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload\0:0:0:0:0:0:0:120140309133432520447.jpg (The filename, directory name, or volume label syntax is incorrect)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(Unknown Source)
java.io.FileOutputStream.<init>(Unknown Source)
org.apache.jsp.fileupload_005fdemo2_jsp._jspService(fileupload_005fdemo2_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.50 logs.
Apache Tomcat/7.0.50
这是报错信息
3 个解决方案
#1
0:0:0:0:0:0:0:120140309133432520447.jpg
换个文件名试试。这个文件名是不合法的
换个文件名试试。这个文件名是不合法的
#2
java.io.FileNotFoundException: F:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload\0:0:0:0:0:0:0:120140309133432520447.jpg
文件没找到。
文件没找到。
#3
文件找不到……
#1
0:0:0:0:0:0:0:120140309133432520447.jpg
换个文件名试试。这个文件名是不合法的
换个文件名试试。这个文件名是不合法的
#2
java.io.FileNotFoundException: F:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload\0:0:0:0:0:0:0:120140309133432520447.jpg
文件没找到。
文件没找到。
#3
文件找不到……