"Java code"--by modifying response to reach the download file or to display the picture in a browser

Source: Internet
Author: User
Preface

Local download files have actually been contacted before, but there is always the question is that the download must open a new window, through the AJAX request can not achieve the purpose of downloading files, so generally for the exception to capture my custom reaction in the interface I usually write another interface, take into account all kinds of exceptions, and then execute the new download command. If there is a better way to tell me, this article is mainly about downloading files in two ways and the picture in the browser. Of course, these two implementations are inseparable from the response settings. content Show pictures by browser

At present, the company has this demand, the picture is downloaded to the local, but not on the image server, also no longer the root of the project, how to find the picture, and the picture into the IMG tag, this way can be achieved

@ResponseBody @RequestMapping (value = "/test.action") public void Base64tofile (String base64, String fileName,
        HttpServletResponse response) {file file = null;
        InputStream FIS = null;
        OutputStream fos = null; try {file = new file ("G:/image/2.jpg")//assumes the path of the picture FIS = new Bufferedinputstream (New Fileinputstre
            AM (file));
            byte[] buffer = new byte[fis.available ()];
            Fis.read (buffer);
            Sets the header Response.reset () of the response;
            Response.setheader ("Content-type", "image/jpg");
            Response.AddHeader ("Content-length", "" "+ File.length ());
            FOS = new Bufferedoutputstream (Response.getoutputstream ());
            Fos.write (buffer);
        Response.flushbuffer ();
        catch (Exception e) {e.printstacktrace ();
                finally {if (FIS!= null) {try {fis.close ();catch (IOException e) {e.printstacktrace ();
                } if (fos!= null) {try {fos.close ();
                catch (Exception E2) {e2.printstacktrace (); }
            }
        }
    }

The pictures in the browser are shown below
Download Files

1. This is my first contact in the way, where the use of Httpheaders and Httpstatus are included in the Springmvc class, are some of the encapsulated enumeration class, if you want to know more about these things can go online reference blog

@RequestMapping (value = "/downfile.action") public responseentity<byte[]> Downfile () {httpheaders Heade
        rs = new httpheaders ();
        InputStream FIS = null;
        byte[] buffer = NULL;
            try {String filepath = "g:/image/2.jpg";
            File File = new file (filepath);
            String filename = File.getname ();
            Headers.setcontentdispositionformdata ("attachment", filename);
            FIS = new Bufferedinputstream (new FileInputStream (file));
            Buffer= new Byte[fis.available ()];
            Fis.read (buffer);
        return new responseentity<byte[]> (Buffer,headers,httpstatus.ok);
            catch (Exception e) {e.printstacktrace ();
        return new responseentity<byte[]> (Buffer,headers,httpstatus.not_found);
                finally {if (FIS!= null) {try {fis.close (); catch (IOException e) {E.printstackTrace (); }
            }
        }

    }
Summary

In fact, when we understand these things, we can quickly give a solution to the abnormal situation of the browser, to speed up the development of Web pages is very helpful, life-long learning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.