读取远程服务器文件并输出文件

  public static void main(String[] args) throws Exception {
        String path="https://htmlstoretest.oss-cn- 
       hangzhou.aliyuncs.com/acf/test100110010107011301781a9b73-ac34-479b-b793- 
       2093bc1b94eb.pdf";
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = connection.getInputStream();


        byte[] originalBytes = parse(inputStream).toByteArray();

     
        try {
            File originalFile = new File("d:origin.pdf");
       
            Files.write(originalBytes, originalFile);
      
        } catch (IOException fileIoEx) {

        }


 public static ByteArrayOutputStream parse(InputStream in) throws Exception {
        final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = in.read()) != -1) {
            swapStream.write(ch);
        }
        return swapStream;
    }

 

你可能感兴趣的:(读取远程服务器文件并输出文件)