public void the download (it request, HttpServletResponse response, String path) {
Try {
//path refers to want to download a file path here wrote an absolute path
File the File=new File (" F: \ \ wordTest \ \ 1380336710055714818 purchase requisition. Docx ");
//get the file name
String filename=file.getName();
//get file suffix
String ext=filename. The substring (filename. LastIndexOf (". ") + 1). The toLowerCase ();
//the file is written to the input stream
FileInputStream FileInputStream=new FileInputStream (file);
InputStream fis=new BufferedInputStream (fileInputStream);
Byte [] buffer=new byte [fis. Available ()];
Fis. Read (buffer);
Fis. Close ();
OutputStream OutputStream=new BufferedOutputStream (response. GetOutputStream ());
//to empty the response
response.reset();
//set the response Header
response.setCharacterEncoding("UTF-8");
The response. AddHeader (" the Content - the Disposition ", "legislation; Filename="+ URLEncoder. Encode (filename," utf-8 "));
Response. AddHeader (" the Content - Length ", "" + file. The Length ());
The response. SetContentType (" application/octet stream - ");
OutputStream. Write (buffer);
OutputStream. Flush ();
{} the catch (IOException ex)
ex.printStackTrace();
}
}
Then run error
Breakpoint with to the outputStream. Write (buffer) found an error, ask bosses give directions
CodePudding user response:
The error is: could not findacceptable representation.In my local environment I run this code, without exception,
So I guess this may be because the spring version is different due to the problem,
I find from the Internet, the problem appeared in the version 3.2 and later, most of the 3.1 version without the problem,
Speculation method: add a return value to the method, the return value of short duration is not null;
If not, you can consult the blog
https://blog.csdn.net/helloman0908/article/details/84654341