JAVA read file stream, set browser download or preview directly

Recent projects need to preview images via URL in the browser. But found that the browser always defaults to download, not preview. I did some research and found the problem:

// Set the Header of the response, pay attention to this sentence, if it is enabled, the default browser will perform the download operation. If it is commented out, the browser will preview it by default.
 response.addHeader("Content-Disposition", "attachment;filename=" + FileUtil.getOriginalFilename(path));

Then you need to pay attention:

response.setContentType(contentType);//Different file types have different contentTypes, such as images are generally image/jpeg, image/png, etc.
  @RequestMapping(value = "getFile/{folder}/{fileName:.+}*", method = RequestMethod.GET)
    public void getFile(HttpServletResponse response, @PathVariable String folder,
                        @PathVariable String fileName)
    {
        // set encoding
        response.setCharacterEncoding("UTF-8");
        try
        {

            String path = folder + "/" + fileName;
            boolean flag = ossClient.doesObjectExist(ossProperties.getBucket(), path);

            // Check if the file exists
            if (flag)
            {
                // clear the response
                response.reset();
                // Set the Header of the response, pay attention to this sentence, if it is enabled, the default browser will perform the download operation. If it is commented out, the browser will preview it by default.
                // response.addHeader("Content-Disposition",
                // "attachment;filename=" + FileUtil.getOriginalFilename(path));
                // response.addHeader("Content-Length", "" + buf.length);
           
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                // ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
                OSSObject ossObject = ossClient.getObject(ossProperties.getBucket(), path);

                String contentType = ossObject.getObjectMetadata().getContentType();
                System.out.println(contentType);
                //Note the contentType type
                response.setContentType(contentType);

                byte[] buf = new byte[1024];
                InputStream in = ossObject.getObjectContent();

                int L;
                while ((L = in.read(buf)) != -1)
                {
                    // if (buf.length != 0)
                    // {
                    toClient.write(buf, 0, L);
                    // }
                }
                in.close();
                // Close the file stream after writing
                toClient.flush();
                toClient.close();
                // response.getOutputStream().write(bos.toByteArray());
            }
            else
            {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "The related resource could not be found");
            }

        }
        catch (IOException e)
        {
            e.printStackTrace ();
        }
    }


Guess you like

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