SmartUpload组件使用过程中遇到的错误
在根据《Java Web开发实战经典》尝试为上传文件自动命名是遇到如下错误:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: org.lxh.smart.SmartUploadException: File can't be saved (1120). org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: org.lxh.smart.SmartUploadException: File can't be saved (1120). org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) org.apache.jsp.smartupload_005fdemo02_jsp._jspService(smartupload_005fdemo02_jsp.java:116) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
org.lxh.smart.SmartUploadException: File can't be saved (1120). org.lxh.smart.File.saveAs(File.java:75) org.lxh.smart.File.saveAs(File.java:54) org.apache.jsp.smartupload_005fdemo02_jsp._jspService(smartupload_005fdemo02_jsp.java:88) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
Apache Tomcat/6.0.14
错误1:原书中函数getIPTimeRand()
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();
}
定义有误:可以修改为String s[] = this.ip.split("."); 但这样,保存的文件名不包含IP中的字段
错误2 :smartupload_demo02.jsp中
<img src="../upload/<%=fileName%>">应改为
<img src="./upload/<%=fileName%>">
这样就可以在页面内看到上传的图片了。