请教 java.net.SocketTimeoutException: Read timed out
悟影 2007-04-29 10:25:09 上传文件代码bean
/**
* @author lichuxiong
*/
package arch.upload;
import java.io.*;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletInputStream;
public class UpFileThread implements Runnable{
HttpServletRequest request;
HttpServletResponse response;
private boolean state;//线程是否已经启动
private boolean running;//是否正在运行
private boolean complete;//是否已经完成
private int percent;
public UpFileThread() {
super();
state=false;
running=false;
complete=false;
percent=0;
}
/**
* 初始化时把页面的request和response传递进来
* @param request
* @param response
*/
public UpFileThread(HttpServletRequest request,HttpServletResponse response) {
super();
this.request=request;
this.response=response;
state=false;
running=false;
complete=false;
percent=0;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public synchronized boolean isComplete() {
return complete;
}
public synchronized void setComplete(boolean complete) {
this.complete = complete;
}
public synchronized int getPercent() {
return percent;
}
public synchronized void setPercent(int percent) {
this.percent = percent;
}
public synchronized boolean isRunning() {
return running;
}
public synchronized void setRunning(boolean running) {
this.running = running;
this.setState(running);
}
public synchronized boolean isState() {
return state;
}
public synchronized void setState(boolean state) {
this.state = state;
}
/**
* 判断能不能上传 后缀名,文件大小
* @param suffix
* @param size
* @return
*/
public boolean canUpload(String suffix,int size){
//System.out.println(suffix.toLowerCase());
//System.out.println(size);
Suffix suf=new SearchSuffix().getSuffixMember(suffix.toLowerCase());
if(suf!=null&&suf.getSize().intValue()>=size){
return true;
}
return false;
}
/**
* 文件上传主要方法
* @throws ServletException
* @throws IOException
*/
public synchronized void up() throws ServletException, IOException{
String readLine;
byte buf[]=new byte[512];
ServletInputStream sis=request.getInputStream();
int upFileSize=request.getContentLength();
//开始读取
int readLength=sis.readLine(buf,0,512);//去掉分隔行
String boundary=new String(buf,0,readLength);
int boundaryLength=boundary.length();//因为下面字节读取时会把回车换行符也读入
readLength=sis.readLine(buf,0,512);
readLine=new String(buf,0,readLength);
int index=readLine.indexOf("filename=");//查找文件名
String name=readLine.substring(index+10);
name=name.substring(0, name.length()-3);//去掉末尾的分号,获得完整的文件名(注意中文名的显示)
System.out.println(name);
//查找文件后缀
String suffix=name.substring(name.lastIndexOf(File.separator));
suffix=suffix.substring(suffix.indexOf("."));
//判断相应文件能不能传(后缀,大小)
if(!canUpload(suffix, upFileSize))
return ;
//保存文件大小到session,供页面调用
//request.getSession().setAttribute("upFileSize", new Integer(upFileSize));
//设定新文件名
String newName=Long.toString((new Date()).getTime());
newName=newName+suffix;
//设定文件 输出流
String path=request.getRealPath("/")+"upfiles/";
FileOutputStream out=new FileOutputStream(path+newName);
int upedSize=0;
//request.getSession().setAttribute("upedSize", new Integer(upedSize));
try{
//寻找文件起点
while ( (readLength = sis.readLine(buf, 0, buf.length)) != -1) {
String s = new String(buf, 0, readLength);
if ( (index = s.indexOf("Content-Type:")) != -1) {
break;
}
}//while
sis.readLine(buf, 0, buf.length);//去掉一空行
//读文件数据
while((readLength = sis.readLine(buf, 0, buf.length)) != -1){
upedSize+=readLength;
this.setPercent((int)((upedSize/upFileSize)*100));//设置完成的百分数
if ( (buf[0] == 45) && (buf[1] == 45) && (buf[2] == 45) && (buf[3] == 45) && (buf[4] == 45)) {//若读结束
break;
}
out.write(buf,0,readLength);
//out.flush();
}
this.setPercent(100); //设置完成100
this.setComplete(true); //设置完成标志
}catch(Exception e){
e.printStackTrace();
File tmp=new File(path+newName);
tmp.delete();
}finally{
out.close();
sis.close();
this.setRunning(false); //设置线程已经停止
}
}
public void run(){
try{
setRunning(true); //启动线程
setComplete(false); //还没完成
setPercent(0); //完成的了0%
up(); //进入工作状态,上传
}catch(Exception e){
e.printStackTrace();
}
}
}
网页调用处代码
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage=""%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>文件上传</title>
</head>
<body>
<%session.removeAttribute("upFileBean"); %>
<jsp:useBean id="upFileBean" scope="session" class="arch.upload.UpFileThread"/>
<%
upFileBean.setRequest(request);
upFileBean.setResponse(response);
new Thread(upFileBean).start();
%>
<jsp:forward page="upstatus.jsp"/>
</body>
</html>
重启tomcat后第一次不管传什么文件都能正确上传
但接下来传就出现如题的问题
请高手指点一二。谢谢