使用HttpClient实现文件的上传

来源:互联网 发布:专业调色软件app 编辑:程序博客网 时间:2024/05/17 09:31
public void upload(String localFile){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = HttpClients.createDefault();
            
            // 把一个普通参数和文件上传给下面这个地址 是一个servlet
            HttpPost httpPost = new HttpPost(URL_STR);
            
            // 把文件转换成流对象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)
                    .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();
            }
        }

    }




转自:http://www.cnblogs.com/Scott007/p/3817285.html


注,现在这种水下操作门坎越来越高了,都会检查cookie,有的还要检查Referer



补充一个,

//上传文件

        FileBody bin = new FileBody(imgFile);
        HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).build();  


//这个是上传byte[]数组的用法,

前端上传文件转成了base64,到server端,把base64 转回来,decode后,是个byte[]数组,并不想把这个byte[]数组在本地或者数据库临时存成文件,而是直接转发,上传到另一台服务器,用一下方法实现


     HttpPost httpPost = new HttpPost(url);

            String contentType = "application/json";
            HttpEntity reqEntity = MultipartEntityBuilder.create().addBinaryBody("file", bytes, ContentType.create(contentType),fileName).build();
            httpPost.setEntity(reqEntity);




0 0