使用jspsmartupload实现上传文件中的一个问题
我开始用 Tomcat 5.0.28作为服务,然后使用jspsmartupload来实现文件的上传,开始设置测试成功.但是重新启动服务器后就出问题了,换用Tomcat5.5.20后也是不行.请高手给看一下.
我的负责接收上传的文件jsp 代码是:
<%@ page import = "com.jspsmart.upload.SmartUpload,java.util.*,java.io.* " %>
<%
int count=0;
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
try{
int count=su.save( "/upload/ ");
if (count==1) {out.print( "sucess ");}
else out.print( "Failed ");
}
catch(Exception e)
{
out.println(e.toString());
}
out.print( "size: "+su.getSize());
%>
日志错误为:
2007-2-4 17:43:25 org.apache.catalina.core.ApplicationContext log
淇℃: ContextListener: contextInitialized()
2007-2-4 17:43:25 org.apache.catalina.core.ApplicationContext log
淇℃: SessionListener: contextInitialized()
2007-2-4 17:43:25 org.apache.catalina.core.ApplicationContext log
淇℃: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]]
不知道有人遇到过同样的问题吗?
谢谢 .
[解决办法]
建议你改用uploadbean,上载到本地目录没有问题,而且支持中文表单提交。
<%@ page contentType= "text/html;charset=gb2312 " %>
<head>
<title> fbysss UploadBean 示例 </title>
</head>
<form name= "form1 " METHOD= "POST " ACTION= "3.jsp " ENCTYPE= "multipart/form-data ">
<input name= "title " type= "text " value= "中文字 ">
<td class= "bodystyle "> 附件 </td>
<td class= "bodystyle ">
<input name= "attach " type= "FILE " id= "attach " size= "50 " > </td>
<input name= "ok " type= "submit " value= "提交 ">
</form>
调用uploadbean
<!--
//==========================================================================
//文件:UploadBean上传实例
//功能:解决中文乱码,完成文件上传,并提供上传改名解决方案
//作者:fbysss
//msn:jameslastchina@hotmail.com
//==========================================================================
-->
<%@ page contentType= "text/html;charset=GBK " %>
<%@ page language= "java " import= "com.jspsmart.upload.* "%>
<%@ page import= "java.text.SimpleDateFormat "%>
<%@ page import= "java.io.File "%>
<%@ page import= "java.util.* "%>
<%@ page import= "javazoom.upload.* "%>
<%@ page import= "uploadutilities.FileMover "%>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>
<%
request.setCharacterEncoding( "GBK ");//设置编码格式,就不用一个个转码了。
FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
UploadBean upBean = new UploadBean();
MultipartFormDataRequest mrequest = null;
Hashtable files = null;
if (MultipartFormDataRequest.isMultipartFormData(request))
{
mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER, "GBK ");//注意这里也要设置编码参数
//String sTt0 = mrequest.getParameter( "title ");
//out.println( " <br> Title0是: "+sTt0+ " <br> ");
//这里用来测试title参数是否正确。调试的时候,加一句if (true)return;即可。
files = mrequest.getFiles();
}
//String sWebRootPath = request.getRealPath( "/ ");//得到你的web应用的根。
//String sPath=sWebRootPath+ "attach ";
//或者
String sPath= "c:/u ";
int iFileCount = 0;
String sLocalFileName = " "; //原文件名
String sServerFileName= " ";//新文件名
//文件获取
if ( (files != null) || (!files.isEmpty()) ) {
iFileCount = files.size();
UploadFile file = (UploadFile) files.get( "attach ");
sLocalFileName=file.getFileName();
out.println( "sLocalFileName: "+sLocalFileName);
int ii= sLocalFileName.indexOf( ". "); //取文件名的后缀
String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
//得到不重复的文件名
java.util.Date dt = new java.util.Date(System.currentTimeMillis());
SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMddHHmmssSSS ");
sServerFileName= fmt.format(dt);
sServerFileName =sServerFileName + sExt;
//如果不存在该目录,则新建一个
File dir =new File(sPath);
if (!dir.exists()){
out.println( "dir1 doesnot exist ");
out.println( "dir1 exist now ");
dir.mkdirs();
}else{
out.println( "dir1 has existed ! ");
}
upBean.setFolderstore(sPath);//设置要上传的目录
System.out.println( "1 ");
upBean.addUploadListener(fileMover);//增加filMover监听
System.out.println( "2 ");
fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
System.out.println( "3 ");
upBean.store(mrequest, "attach ");//上传
out.println( "file path is "+sPath+ "/ "+sServerFileName);
}
%>