Java downloads the network file sample code with a file stream

  • 2020-04-01 02:31:51
  • OfStack


public HttpServletResponse download(String path, HttpServletResponse response) {
        try {
            //Path is the path to the file you want to download.
            File file = new File(path);
            //Get the file name.
            String filename = file.getName();
            //Gets the suffix name of the file.
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            //Download the file as a stream.
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            //To empty the response
            response.reset();
            //Set the Header for response
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

    public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
        //Download local files
        String fileName = "Operator.doc".toString(); //The default save name for the file
        //Read to the stream
        InputStream inStream = new FileInputStream("c:/Operator.doc");//File location
        //Format the output
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """);
        //Loop out the data in the stream
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void downloadNet(HttpServletResponse response) throws MalformedURLException {
        //Download network files
        int bytesum = 0;
        int byteread = 0;

        URL url = new URL("windine.blogdriver.com/logo.gif");

        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream("c:/abc.gif");

            byte[] buffer = new byte[1204];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


Related articles: