(file download, preview) the difference between inputstream and outputstream

 

inputstream--"input, which is used to turn data into a stream and transmit it in a pipeline

outputstream--"output, which is used to undertake the data in the pipeline and output it to the corresponding place

The outputstream is generated by the path and written to this path, and is generated by the response and written on the page, that is, download or preview

If the header is set, it is download. If it is not set, it will be previewed as long as the browser can parse it (this will happen if the suffix name is not specified, but it is invalid for word and excel). Automatically let you choose to browse or download

 

/**

* document dowload

* This method is used for downloading (Information Information - Diangang Weekly)

* Configure this path filtering validation in shiro 

* @param request

* @param response

* @param path

* @param fileName

* @return String

*/

@RequestMapping(value = "/downloadInvoiceFile")

public String downloadInvoiceFile(HttpServletRequest request, HttpServletResponse response, String path, String fileName) {

 

if (StringUtils.isNull(path)) {

return null;

}

if (StringUtils.isNull(fileName)) {

fileName = "Attachment";

}

try {

response.reset();

response.setContentType("MIME");/////

//response.setContentType("MIME");//Automatic identification of suffix names, but not allowed for word and excel

response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes("GB2312"), "ISO_8859_1")+path.substring(18) + "\"");

//The name set here is the name and suffix name of the attachment (the suffix name of any file is set accurately), pay attention to the Chinese to be transcoded, otherwise there will be garbled characters or invalid settings

tfsManager.fetchFile(path, "", response.getOutputStream());

response.flushBuffer();

} catch (IOException e) {

e.printStackTrace ();

}

return null;

}

 

 

========================= Preview

(For browser download previews you can choose (except for zip and other browsers that know that they can't preview)) =======================

  /*

  * The delivery note is displayed

  */

   @RequestMapping(value = "/showPic")

public void showPic(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {

String path = request.getParameter("path");

response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(fileName.getBytes("GB2312"), "ISO_8859_1")+path.substring(18) + "\"");

       tfsManager.fetchFile(path, "", response.getOutputStream());

}

   

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326225286&siteId=291194637