Solve the problem of springmvc file download and content corruption

Problem Description:

    In java, the inputstream stream is converted into a string, and then the String conversion will be inputStream, and the content of the downloaded file is damaged, such as downloading a word document


scenes to be used:

    The underlying service reads the file content and obtains the InputStream, because multiple interface calls are required. In order to facilitate data transfer, the InputStream is converted into a String string for transmission. The upper-layer service calls the interface to obtain the String string, and then converts it into an InputStream for IO reading. write operation;

problem causes:

    If the file content is character type, there is no problem with this method. If it is not character type, such as MP3, picture, word document, etc., it will not be able to be opened after downloading, as shown above;

Solution:

    The binary data is base64 encrypted before the underlying service InputStream stream is converted to String, and then converted to 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 encryption
        return Base64.encodeBase64String(buffer);
	}

Then the upper-layer service calls the interface to obtain the string, and then performs base64 decryption:

Map<String, Object> reMap = gitCodeViewService.gitCodeView(Id, path, version);
String content = (String) reMap.get("content");
			
// decode with base64
byte[] decodeByte = Base64.decodeBase64(content);
//Convert decoded binary to inputStream
InputStream is = new ByteArrayInputStream(decodeByte);

When using InputStream for IO read and write operations, it is normal to download the file content.

Download file code:

String content = (String) codeViewMap.get("content");
			
// decode with base64
byte[] decodeByte = Base64.decodeBase64(content);
//Convert decoded binary to inputStream
InputStream is = new ByteArrayInputStream(decodeByte);
			
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {  
    // IE browser processing
    fileName = java.net.URLEncoder.encode(fileName, "UTF-8");  
    } else {  
    // Processing of non-IE browsers:  
    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");  
   }
			
// Set the file header: the last parameter is to set the download file name
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
// Set the file ContentType type, this setting will automatically determine the download file type  
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
// copy input stream and output stream
int len ​​= 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) > 0) {
	os.write(b, 0, len);
}
os.close();
is.close();



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326669585&siteId=291194637