HttpPost發送文件類型數據


1、引入jar包

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>    

2、客戶端

public void upload(String localFile){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = HttpClients.createDefault();

            // 把一個普通參數和文件上傳給下面這個地址 是一個servlet
            HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/processUpload");

            // 把文件轉換成流對象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()
                    // 相當於<input type="file" name="file"/>
                    .addPart("file", bin)
                    // 相當於<input type="text" name="userName" value=userName>
                    .addPart("userName", userName)
                    .addPart("pass", password)
                    .addPart("memberId",password)
                    .build();

            httpPost.setEntity(reqEntity);

            // 發起請求 並返回請求的響應
            response = httpClient.execute(httpPost);

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

            // 獲取響應對象
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                // 打印響應長度
                System.out.println("Response content length: " + resEntity.getContentLength());
                // 打印響應內容
                System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
            }
            // 銷毀
            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();
            }
        }
    }

3、服務端

  @RequestMapping("/processUpload")
    public void processUpload(HttpServletRequest request, HttpServletResponse response, SendRequestVo sendRequestVo, @RequestParam("file") MultipartFile file) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            String name1 = file.getName();
            byte[] bytes = file.getBytes();
            FileUtils.writeByteArrayToFile(new File("C:\\Users\\wangdongjiang\\Desktop\\wdj\\" + name1), bytes);
            response.addHeader("token", "hello");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM