byte[] buffer = new byte[in.available()];
int i = -1;
while ((i = bis.read(buffer)) != -1) {
//response.getOutputStream().write(buffer,0, i);
logger.info(buffer);
}
String s = new String(buffer); //获取的文件里的文本内容
String ss = changeText(s); //将文本内容修改了一下
InputStream fileInputStream = new ByteArrayInputStream(ss.getBytes("gbk"));
bis = new BufferedInputStream(fileInputStream);
byte[] bytes = new byte[bis.available()];
ServletOutputStream outputStream = response.getOutputStream();
while ((i = bis.read(bytes)) != -1) {
outputStream.write(bytes,0, i);
outputStream.flush();
}
//outputStream.write(changeText(s).getBytes("gbk"));
// outputStream.flush();
//outputStream.close();
Java从文件中读取了byte[] 转换成String 修改String以后 再转换成byte[]进行下载 文件内容显示不全
原来的内容

修改后缺了一截

修改后的ss中的值,正常吗
正常的 我打断点显示的就是我要的内容
byte[] bytes = new byte[1024];
换成这个。
还是一样
你写入有问题,你试着写入到本地文件中试试。不要响应
我试试
有个问题,类型你为啥要转来转去的
public void copyFile() throws IOException {
File f = new File("E:\\a.java");
FileInputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(f);
os = new FileOutputStream(new File("D:\\a.java"));
byte[] b = new byte[is.available()];
int len = 0;
while ((len=is.read(b))!=-1){
os.write(b,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
os.close();
is.close();
}
}
outputStream.flush方法要放到while循环的外面