这是我写的上传文件的代码:
package com.lm.updownload;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;public class UpLoad { /**
 * 上传文件
 * @param pageContext
 * @param response
 * @param request
 * @param file_maxSize
 * @param allowTypes
 * @param notallowTypes
 * @param tiaozhuanURL
 */
public static void upLoad(PageContext pageContext,HttpServletResponse 
response,HttpServletRequest request,
long file_maxSize, String allowTypes,String notallowTypes,String tiaozhuanURL){
PrintWriter out=null;
try {
out = response.getWriter();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

SmartUpload mySmartUpload = new SmartUpload();

String  ext = "";
String url = "/upload/"; //应保证在根目录中有此目录的存在
//初始化
try {
mySmartUpload.initialize(pageContext);
} catch (ServletException e1) {
e1.printStackTrace();
}
try {
//只允许上载此类文件
if(!("".endsWith(allowTypes)))
    mySmartUpload.setAllowedFilesList(allowTypes);
//设定禁止上传的文件
if(!("".endsWith(notallowTypes)))
    mySmartUpload.setDeniedFilesList(notallowTypes);
//上载文件 
mySmartUpload.upload();
} catch (Exception e) {
out.print("<SCRIPT language=\'javascript\'>");
out.print("alert('不支持上传该类型文件');");
out.print("window.location=\'"+tiaozhuanURL+";\'");
out.print("</SCRIPT>");
}
try {
File myFile = mySmartUpload.getFiles().getFile(0);
if (myFile.isMissing()) {
//System.out.println("请先选择要上传的文件");
out.print("<SCRIPT language=\'javascript\'>");
out.print("alert('请选择要上传的文件');");
out.print("</SCRIPT>");

} else {
String myFileName = myFile.getFileName(); //取得上载的文件的文件名
myFileName=new String(myFileName.getBytes(),"utf-8");//转码 解决中文文件名乱码
ext = myFile.getFileExt(); //取得后缀名
int file_size = myFile.getSize(); //取得文件的大小  
String saveurl = ""; if (file_size < file_maxSize) { 
String filename = myFileName;
saveurl = request.getRealPath("/") + url;
saveurl += filename;
System.out.println("路径:"+saveurl);
myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);

out.print("<SCRIPT language=\'javascript\'>");
out.print("alert('上传文件成功');");
out.print("window.location='"+tiaozhuanURL+"';");
out.print("</SCRIPT>");
} else {
out.print("<SCRIPT language=\'javascript\'>");
long fileSize=file_maxSize / 1000;
if(fileSize >= 1024)
   out.print("alert('上传文件大小不能超过" + (file_maxSize / 1000000)+ "M');");
else
   out.print("alert('上传文件大小不能超过" + (file_maxSize / 1000)+ "K');");
out.print("window.location='"+tiaozhuanURL+"';");
out.print("</SCRIPT>");    }
}
} catch (Exception e) {
System.out.println("报错了,错误是:");
e.printStackTrace();
out.print("<SCRIPT language=\'javascript\'>");
out.print("alert('上传文件失败!');");
out.print("window.location=\'mainPage.jsp;\'");
out.print("</SCRIPT>");
}
}}<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="com.lm.updownload.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文件的上传与下载</title>
</head>
<body>
  <%
     UpLoad.upLoad(pageContext,response,request,10000000,"","","mainPage.jsp");
    
   %>
</body>
</html>
运行时,有的能上传成功,有的不行。后来我发现成功与否跟上传的文件名有关系:
比如上传 "兵王.txt",上传成功。
路径:C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.5\webapps\upLoadTest\/upload/兵王.txt但是上传 "搜神记.txt",则报错:
路径:C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.5\webapps\upLoadTest\/upload/搜神??txt
报错了,错误是:
com.jspsmart.upload.SmartUploadException: File can't be saved (1120).
at com.jspsmart.upload.File.saveAs(File.java:108)
at com.lm.updownload.UpLoad.upLoad(UpLoad.java:83)
at org.apache.jsp.upLoad_jsp._jspService(upLoad_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:332)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)其他的文件类型类似,貌似跟上传文件的文件名的字数有关?这什么情况

解决方案 »

  1.   

    可能是文件大小的问题,smartupload有上传上限的,好像是几十M来着,记不清了
      

  2.   

    你换这个包试一下吧。
    upload_jelinjdk1.6.jar
      

  3.   

    upload_jelinjdk1.6.jar,找不到这个包啊。
    这个包有什么特别之处吗?
      

  4.   

    是不是获取文件名的时候问题啊
    我用jxl做过上传文件的 好像没有这个问题
      

  5.   

    String myFileName = myFile.getFileName(); //取得上载的文件的文件名
    myFileName=new String(myFileName.getBytes(),"utf-8");//转码 解决中文文件名乱码
    ext = myFile.getFileExt(); //取得后缀名
    int file_size = myFile.getSize(); //取得文件的大小   
    String saveurl = "";if (file_size < file_maxSize) {  
    String filename = myFileName;
    saveurl = request.getRealPath("/") + url;
    saveurl += filename;
    我说句题外话:lz你写的code也太是不是还是学生?
    1、myFileName你确定没有乱码?
    我看你的错误:
    但是上传 "搜神记.txt",则报错:
    路径:C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.5\webapps\upLoadTest\/upload/搜神??txt你debug,看看。
    这个文件名是奇数还是偶数肯定没关系。