解决springmvc文件下载,内容损坏问题

2022-08-05 10:39:53

问题描述:

    java 中inputstream流 转成string,再将String转换会inputStream,下载下来的文件,内容损坏,例如下载word文档


使用场景:

    底层服务读取到文件内容获得InputStream,因为需要多次接口调用,为了便于数据传递,将InputStream转换为String字符串进行传递,上层服务调用接口,获取String字符串,在转换成InputStream进行IO的读写操作;

问题原因:

    如果文件内容是字符型,这种方法没有问题,如果不是字符型的,比如MP3,图片,word文档等,下载下来会无法打开,如上图;

解决办法:

    在底层服务InputStream流转换为String前对二进制数据进行base64加密,然后再转为String字符串:

public String inputStream2String(InputStream in) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        byte[] b = new byte[1024];	
        while ((len = in.read(b, 0, b.length)) != -1) {                     
            baos.write(b, 0, len);
        }
        byte[] buffer =  baos.toByteArray();
        //base64加密
        return Base64.encodeBase64String(buffer);
	}

然后上层服务调用接口获得字符串,再进行base64解密:

Map<String, Object> reMap = gitCodeViewService.gitCodeView(Id, path, version);
String content = (String) reMap.get("content");
			
//用base64进行解码
byte[] decodeByte = Base64.decodeBase64(content);
//将解码的二进制文件转换为inputStream
InputStream is = new ByteArrayInputStream(decodeByte);

在使用InputStream进行IO的读写操作,下载文件内容就正常了。

下载文件代码:

String content = (String) codeViewMap.get("content");
			
//用base64进行解码
byte[] decodeByte = Base64.decodeBase64(content);
//将解码的二进制文件转换为inputStream
InputStream is = new ByteArrayInputStream(decodeByte);
			
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {  
    //IE浏览器处理
    fileName = java.net.URLEncoder.encode(fileName, "UTF-8");  
    } else {  
    // 非IE浏览器的处理:  
    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");  
   } 
			
// 设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
// 设置文件ContentType类型,这样设置,会自动判断下载文件类型  
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
// 输入流输出流对拷
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) > 0) {
	os.write(b, 0, len);
}
os.close();
is.close();



  • 作者:你认识小汐吗
  • 原文链接:https://blog.csdn.net/sayoko06/article/details/79793319
    更新时间:2022-08-05 10:39:53