在学习SmartUpload文件上传组件时遇到如下问题,javax.servlet.ServletException: org.lxh.smart.SmartUploadException: File can't be saved (1120).不知如何解决。。。。
现将具体情况描述如下,求高手指导...
上传单个文件,为上传文件自动命名:
(1)表单:01.html
<html>
<head><title>hello</title></head>
<body>
<center>
<form action="smart02.jsp" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="uname"><br>
照片:<input type="file" name="pic"><br>
<input type="submit" value="上传">
<input type="reset" value="重置">
</form>
</center>
</body>
</html>
(2)为上传文件自动命名,格式:IP+时间戳+三位随机数。。编译正常。
package cn.mldn.lxh.util;
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(){ //得到IP地址+时间戳+三位随机数
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();}
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();
}
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());
}
}
(3)smart02.jsp,自动命名。。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="org.lxh.smart.*"%>
<%@ page import="cn.mldn.lxh.util.IPTimeStamp"%>
<html>
<head><title>hello</title></head>
<body>
<%
SmartUpload smart = new SmartUpload();
smart.initialize(pageContext);
smart.upload();
String name = smart.getRequest().getParameter("uname");
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr());
String ext = smart.getFiles().getFile(0).getFileExt();
String fileName = its.getIPTimeRand() + "." + ext;
smart.getFiles().getFile(0).saveAs(getServletContext().getRealPath("/")+"upload"+java.io.File.separator+fileName);
//提示此处异常。。无法保存文件。。。(根目录下已经建立文件及upload)
%>
<h2>姓名:<%=name%></h2>
<img src=\'#\'" /upload/<%=fileName%>">
</body>
</html>
异常信息如下:
type Exception report
message An exception occurred processing JSP page /smartdemo/smart02.jsp at line 15
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 /smartdemo/smart02.jsp at line 15 12: IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()); 13: String ext = smart.getFiles().getFile(0).getFileExt(); 14: String fileName = its.getIPTimeRand() + "." + ext; 15: smart.getFiles().getFile(0).saveAs(getServletContext().getRealPath("/")+"upload"+java.io.File.separator+fileName); 16: 17: %> 18: <h2>姓名:<%=name%></h2> Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: org.lxh.smart.SmartUploadException: File can't be saved (1120). org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794) org.apache.jsp.smartdemo.smart02_jsp._jspService(smart02_jsp.java:86) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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.smartdemo.smart02_jsp._jspService(smart02_jsp.java:69) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.36 logs.