高分帮朋友问个Jsp的问题,多年搞web java的人恐怕都没有做过这样的测试 (300分)

C

Crane

Unregistered / Unconfirmed
GUEST, unregistred user!
是一个通过jsp以流的方式下载文本文件的问题,从调试情况看,当文件
下载时,IE端用户点击保存文件,然后指定该文件的路径和文件名的时候,
从log看,此时该文件的一部分数据已经开始执行传的操作,也就是说
ServletOutputStream out = sRes.getOutputStream();
客户端后还没有指定文件名,服务端就已经执行了若干条out.write(buffer, 0, intLen),
然后程序就停在那里;这就造成如果用户此时取消了这次下载,
多次download又多次取消后IE就会死掉,直到IE被关闭
我想问问有没有方法来避免IE死掉,比如及时发现用户端的取消等等
下面是源码,谢谢了。
package test1;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;public class FileRetriver extends HttpServlet
{
public voiddo
Post(HttpServletRequest sReq ,HttpServletResponse res)
throws IOException,ServletException {
//the choise you give on ur HTML page for the file to bedo
wnloaded
String fileWanted = "xyz.txt";
String xxx = getServletContext().getRealPath("/");
System.out.println("xxx " + xxx);
String pathOfFile = xxx + "Downloadablefiles"+"/" + fileWanted;
res.setContentType("application/x-msdownload");
res.setHeader("Content-disposition", "attachment;filename=" + fileWanted );
ServletOutputStream out = res.getOutputStream ();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(pathOfFile));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
int allLng = 0;
System.out.println("while in ");
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
allLng += bytesRead;
System.out.println("while ********* " + allLng);
bos.write(buff, 0, bytesRead);
}
System.out.println("while out ");
} catch(final IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null){
bis.close();
System.out.println("bis.close() ");
}
if (bos != null){
bos.close();
System.out.println("bos.close() ");
}
}
}
}
 
试试SmartUpload(有下载功能),或其他组件。
 
kaka, crane你在这儿也问啊?
我占个座儿 ^_^
 
啊,刚看到,拿分来,快点.
 
就不给小猪,hiahiahia
 
多人接受答案了。
 
我不知道jsp的内容,但是我在其他应用当中,采取
每次发送8192字节,每次在发送前检查socket是否已经断开或者超时!
我利用这个还可以发送专门的27M的图片和文件,可以看到IE滚动条的慢慢滚动!
 
顶部