Use HttpClient to upload and download files

  • 2020-05-26 08:21:06
  • OfStack

1 HTTP

The HTTP protocol is probably the most used and important protocol on Internet today, and more and more Java applications need to access network resources directly through the HTTP protocol.

While basic access to the HTTP protocol is already provided in the java.net package of JDK, the JDK library itself is not rich and flexible enough for most applications. HttpClient is designed to provide an efficient, up-to-date, and feature-rich client programming toolkit that supports the HTTP protocol, and it supports the latest versions and recommendations of the HTTP protocol.

In most cases, we use Chrome or other browsers to access an WEB server, which can be used to browse the page, view information or submit some data, upload or download files, etc. Some of these pages are just plain old pages, some require users to log in before they can use them, or require authentication, and some are transmitted encrypted, such as HTTPS. None of these situations are a problem with the browsers we currently use. But what if we need to access the server's resources without a browser? So what to do?

Let's do a small Demo for the example of uploading and downloading files initiated by the local client. HttpClient has two forms, 1 kind is org. apache. http, 1 kind is org. apache. commons. httpclient. HttpClient.

2 file upload

File uploads can be done in two ways: PostMethod and HttpPost. The treatment is much the same. PostMethod USES FileBody to wrap the file stream, and HttpPost USES FilePart to wrap the file stream. When passing a file stream to a server, other parameters can be passed at the same time.

2.1 client processing

2.1.1 PostMethod way

Wrap the file into FilePart and put it into the Part array. At the same time, other parameters can be put into StringPart. The HttpClient here is org apache. commons. httpclient. HttpClient.


public void upload(String localFile){
    File file = new File(localFile);
    PostMethod filePost = new PostMethod(URL_STR);
    HttpClient client = new HttpClient();
    
    try {
      //  You can simulate page parameter submission by following methods 
      filePost.setParameter("userName", userName);
      filePost.setParameter("passwd", passwd);

      Part[] parts = { new FilePart(file.getName(), file) };
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
      
      int status = client.executeMethod(filePost);
      if (status == HttpStatus.SC_OK) {
        System.out.println(" Uploaded successfully ");
      } else {
        System.out.println(" Upload failed ");
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      filePost.releaseConnection();
    }
  }

Remember to release the connection via releaseConnection when you're done.

2.1.2 HttpPost way

This way, similar to the above, only becomes FileBody. The Part array above corresponds to HttpEntity here. The HttpClient here is org apache. http. client. methods.


public void upload(String localFile){
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    try {
      httpClient = HttpClients.createDefault();
      
      //  the 1 Three common parameters and files are uploaded to the following address   is 1 a servlet
      HttpPost httpPost = new HttpPost(URL_STR);
      
      //  Convert the file into a stream object FileBody
      FileBody bin = new FileBody(new File(localFile));

      StringBody userName = new StringBody("Scott", ContentType.create(
          "text/plain", Consts.UTF_8));
      StringBody password = new StringBody("123456", ContentType.create(
          "text/plain", Consts.UTF_8));

      HttpEntity reqEntity = MultipartEntityBuilder.create()
          //  The equivalent of <input type="file" name="file"/>
          .addPart("file", bin)
          
          //  The equivalent of <input type="text" name="userName" value=userName>
          .addPart("userName", userName)
          .addPart("pass", password)
          .build();

      httpPost.setEntity(reqEntity);

      //  The initiating   And returns the response to the request 
      response = httpClient.execute(httpPost);
      
      System.out.println("The response value of token:" + response.getFirstHeader("token"));
        
      //  Get the response object 
      HttpEntity resEntity = response.getEntity();
      if (resEntity != null) {
        //  Print response length 
        System.out.println("Response content length: " + resEntity.getContentLength());
        //  Print response content 
        System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
      }
      
      //  The destruction 
      EntityUtils.consume(resEntity);
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      try {
        if(response != null){
          response.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      
      try {
        if(httpClient != null){
          httpClient.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

2.2 server-side processing

No matter which way the client uploads, the server handles it the same way. After obtaining the parameters through HttpServletRequest, the resulting Item is classified into ordinary forms and File forms.

Through ServletFileUpload, you can set the size and encoding format of the uploaded file.

In short, the server handles the resulting parameters as if they were HTML forms.


public void processUpload(HttpServletRequest request, HttpServletResponse response){
    File uploadFile = new File(uploadPath);
    if (!uploadFile.exists()) {
      uploadFile.mkdirs();
    }

    System.out.println("Come on, baby .......");
    
    request.setCharacterEncoding("utf-8"); 
    response.setCharacterEncoding("utf-8"); 
     
    // Check if there is an upload file  
    boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
     
    if(isMultipart){ 
      DiskFileItemFactory factory = new DiskFileItemFactory(); 
      
      // Specifies the size of the data to be cached in memory , The unit is byte, Here is set to 1Mb 
      factory.setSizeThreshold(1024*1024); 
      
      // Set up the 1 Once the file size exceeds getSizeThreshold() The value when the data is stored on the hard disk directory   
      factory.setRepository(new File("D:\\temp")); 
      
      // Create a new file upload handler 
      ServletFileUpload upload = new ServletFileUpload(factory); 
      
      //  Specifies the maximum size for a single uploaded file , unit : Bytes, let's say 50Mb  
      upload.setFileSizeMax(50 * 1024 * 1024);  
      
      // The specified 1 Total size of multiple files uploaded at a time , unit : Bytes, let's say 50Mb 
      upload.setSizeMax(50 * 1024 * 1024);   
      upload.setHeaderEncoding("UTF-8");
       
      List<FileItem> items = null; 
       
      try { 
        //  parsing request request  
        items = upload.parseRequest(request); 
      } catch (FileUploadException e) { 
        e.printStackTrace(); 
      } 
      
      if(items!=null){ 
        // Parse form project  
        Iterator<FileItem> iter = items.iterator(); 
        while (iter.hasNext()) { 
          FileItem item = iter.next(); 
          
          // If it's a normal form property  
          if (item.isFormField()) { 
            // The equivalent of input the name attribute   <input type="text" name="content"> 
            String name = item.getFieldName();
            
            //input the value attribute  
            String value = item.getString();
            
            System.out.println(" attribute :" + name + "  Attribute values :" + value); 
          } 
          // If you're uploading a file  
          else { 
            // The property name  
            String fieldName = item.getFieldName(); 
            
            // Upload file path  
            String fileName = item.getName(); 
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);//  Get the file name of the uploaded file  
            
            try { 
              item.write(new File(uploadPath, fileName)); 
            } catch (Exception e) { 
              e.printStackTrace(); 
            } 
          } 
        } 
      } 
    } 
    
    response.addHeader("token", "hello");
  }

After processing, the server can set the simple information returned to the client in Header. If the return client is a stream, the size of the stream must be set in advance!

response.setContentLength((int) file.length());

3 file download

Files can be downloaded using HttpClient's GetMethod implementation, as well as HttpGet and the original HttpURLConnection.

3.1 client processing

3.1.1 GetMethod way

The HttpClient here is org apache. commons. httpclient. HttpClient.


public void downLoad(String remoteFileName, String localFileName) {
    HttpClient client = new HttpClient();
    GetMethod get = null;
    FileOutputStream output = null;
    
    try {
      get = new GetMethod(URL_STR);
      get.setRequestHeader("userName", userName);
      get.setRequestHeader("passwd", passwd);
      get.setRequestHeader("fileName", remoteFileName);

      int i = client.executeMethod(get);

      if (SUCCESS == i) {
        System.out.println("The response value of token:" + get.getResponseHeader("token"));

        File storeFile = new File(localFileName);
        output = new FileOutputStream(storeFile);
        
        //  Gets a byte array of network resources , And write to the file 
        output.write(get.getResponseBody());
      } else {
        System.out.println("DownLoad file occurs exception, the error code is :" + i);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if(output != null){
          output.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      
      get.releaseConnection();
      client.getHttpConnectionManager().closeIdleConnections(0);
    }
  }

3.1.2 HttpGet way

The HttpClient here is org apache. http. client. methods.


public void downLoad(String remoteFileName, String localFileName) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    OutputStream out = null;
    InputStream in = null;
    
    try {
      HttpGet httpGet = new HttpGet(URL_STR);

      httpGet.addHeader("userName", userName);
      httpGet.addHeader("passwd", passwd);
      httpGet.addHeader("fileName", remoteFileName);

      HttpResponse httpResponse = httpClient.execute(httpGet);
      HttpEntity entity = httpResponse.getEntity();
      in = entity.getContent();

      long length = entity.getContentLength();
      if (length <= 0) {
        System.out.println(" Download file does not exist! ");
        return;
      }

      System.out.println("The response value of token:" + httpResponse.getFirstHeader("token"));

      File file = new File(localFileName);
      if(!file.exists()){
        file.createNewFile();
      }
      
      out = new FileOutputStream(file); 
      byte[] buffer = new byte[4096];
      int readLength = 0;
      while ((readLength=in.read(buffer)) > 0) {
        byte[] bytes = new byte[readLength];
        System.arraycopy(buffer, 0, bytes, 0, readLength);
        out.write(bytes);
      }
      
      out.flush();
      
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }finally{
      try {
        if(in != null){
          in.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      
      try {
        if(out != null){
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

3.1.3 HttpURLConnection way


public void download3(String remoteFileName, String localFileName) {
    FileOutputStream out = null;
    InputStream in = null;
    
    try{
      URL url = new URL(URL_STR);
      URLConnection urlConnection = url.openConnection();
      HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
      
      // true -- will setting parameters
      httpURLConnection.setDoOutput(true);
      // true--will allow read in from
      httpURLConnection.setDoInput(true);
      // will not use caches
      httpURLConnection.setUseCaches(false);
      // setting serialized
      httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
      // default is GET            
      httpURLConnection.setRequestMethod("POST");
      httpURLConnection.setRequestProperty("connection", "Keep-Alive");
      httpURLConnection.setRequestProperty("Charsert", "UTF-8");
      // 1 min
      httpURLConnection.setConnectTimeout(60000);
      // 1 min
      httpURLConnection.setReadTimeout(60000);

      httpURLConnection.addRequestProperty("userName", userName);
      httpURLConnection.addRequestProperty("passwd", passwd);
      httpURLConnection.addRequestProperty("fileName", remoteFileName);

      // connect to server (tcp)
      httpURLConnection.connect();

      in = httpURLConnection.getInputStream();// send request to
                                // server
      File file = new File(localFileName);
      if(!file.exists()){
        file.createNewFile();
      }

      out = new FileOutputStream(file); 
      byte[] buffer = new byte[4096];
      int readLength = 0;
      while ((readLength=in.read(buffer)) > 0) {
        byte[] bytes = new byte[readLength];
        System.arraycopy(buffer, 0, bytes, 0, readLength);
        out.write(bytes);
      }
      
      out.flush();
    }catch(Exception e){
      e.printStackTrace();
    }finally{
      try {
        if(in != null){
          in.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      
      try {
        if(out != null){
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

3.2 server-side processing

Although the client side handles it differently, the server side is the same.


public void processDownload(HttpServletRequest request, HttpServletResponse response){
    int BUFFER_SIZE = 4096;
    InputStream in = null;
    OutputStream out = null;
    
    System.out.println("Come on, baby .......");
    
    try{
      request.setCharacterEncoding("utf-8"); 
      response.setCharacterEncoding("utf-8"); 
      response.setContentType("application/octet-stream");
      
      String userName = request.getHeader("userName");
      String passwd = request.getHeader("passwd");
      String fileName = request.getHeader("fileName");
      
      System.out.println("userName:" + userName);
      System.out.println("passwd:" + passwd);
      System.out.println("fileName:" + fileName);
      
      // You can pass it on userName and passwd Do into 1 Step processing, such as verifying that the request is valid        
      File file = new File(downloadPath + "\\" + fileName);
      response.setContentLength((int) file.length());
      response.setHeader("Accept-Ranges", "bytes");
      
      int readLength = 0;
      
      in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
      out = new BufferedOutputStream(response.getOutputStream());
      
      byte[] buffer = new byte[BUFFER_SIZE];
      while ((readLength=in.read(buffer)) > 0) {
        byte[] bytes = new byte[readLength];
        System.arraycopy(buffer, 0, bytes, 0, readLength);
        out.write(bytes);
      }
      
      out.flush();
      
      response.addHeader("token", "hello 1");
       
    }catch(Exception e){
      e.printStackTrace();
       response.addHeader("token", "hello 2");
    }finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        }
      }
      if (out != null) {
        try {
          out.close();
        } catch (IOException e) {
        }
      }
    }
  }

4 summary

The most basic function of HttpClient is to implement the Http method. The execution of an Http method involves the interaction of one or more Http requests /Http responses, which is normally handled automatically by HttpClient and transparent to the user. The user only needs to provide the Http request object, and HttpClient will send the http request to the target server and receive the response from the server. If the http request is not executed successfully, httpclient will throw an exception. So pay attention to finally when you're writing code.

All Http requests have one request column (request line), including the method name, the requested URI and Http version Numbers. HttpClient support HTTP / 1.1 the definition of all Http methods: GET, HEAD, POST, PUT, DELETE, TRACE and OPTIONS. The upload above USES Post, and the download is Get.

For now, use org. apache. commons. httpclient. HttpClient 1 more. Look at yourself


Related articles: