当前位置: 代码迷 >> J2SE >> DataOutputStream write有关问题
  详细解决方案

DataOutputStream write有关问题

热度:290   发布时间:2016-04-24 01:48:57.0
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);
}
  相关解决方案