日期:2014-05-20  浏览次数:20794 次

DataOutputStream write问题
response.setContentType("application/octet-stream");
DataOutputStream dos = new DataOutputStream(response.getOutputStream());

String rootPath = ParseProperties.getFilePathByKey("cityListFilePath");// 根路径
rootPath+="/CityJobList.dat";
File fis = new File(rootPath);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fis));
int tmp = -1;
while ((tmp = bis.read()) != -1) {
dos.write(tmp);
}
bis.close();
dos.flush();

if(dos!=null)
dos.close();
以上代码,是读取文件(文件大于10k),然后把读取的文件,打印到客户端,但是客户端接受的数据只能接受小于等于10k的数据!不知道为什么。。。希望各位大侠帮忙解决一下!


------解决方案--------------------
在while循环里
dos.write(tmp);
后面加个dos.flush();试试
------解决方案--------------------
dos.write(tmp);
dos.flush();
}
if(bis!=null)
bis.close();
if(dos!=null)
dos.close();
------解决方案--------------------
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fis));
int tmp = -1;
while ((tmp = bis.read()) != -1) {
dos.write(tmp);
}

改为

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fis));
int tmp = -1;
byte[] br = new byte[1024];
while ((tmp = bis.read(br)) != -1) {
dos.write(tmp,0,br);
}