zoukankan      html  css  js  c++  java
  • Httpclient文件上传

    public static void upload(String url,File file,String filename) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpPost httppost = new HttpPost(url);
                RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
                httppost.setConfig(requestConfig);
                FileBody bin = new FileBody(file);
                StringBody comment = new StringBody(filename, ContentType.TEXT_PLAIN);
                HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("filename", comment).build();
                httppost.setEntity(reqEntity);
                System.out.println("executing request " + httppost.getRequestLine());
                CloseableHttpResponse response = httpclient.execute(httppost);
                try {
                    System.out.println(response.getStatusLine());
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        String responseEntityStr = EntityUtils.toString(response.getEntity());
                        System.out.println(responseEntityStr);
                    }
                    EntityUtils.consume(resEntity);
                } finally {
                    response.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

      

    @PostMapping("/upload")
        public Result upload(@RequestParam("file") MultipartFile file, String filename){
            Result s = new Result(1, "success");
            String usrHome = System.getProperty("user.home");
            try {
                String path = usrHome+"/image/";
                path = path.replace("\","/");
                System.out.println(path);
                File f = new File(path);
                if(!f.exists()){
                    f.mkdirs();
                }
                UploadUtils.uploadFileTest(file,path,filename);
            }catch (Exception e){
                s.setCode(0);
                s.setMessage("失败");
            }
            return s ;
        }
    

      

    public static  void uploadFileTest(MultipartFile zipFile,String targetFilePath,String fileName) {
            File targetFile = new File(targetFilePath + File.separator + fileName);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(targetFile);
                IOUtils.copy(zipFile.getInputStream(), fileOutputStream);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                }
            }
        }
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
    </dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.3</version>
    </dependency>
  • 相关阅读:
    unity3d连接Sqlite并打包发布Android
    EasyTouch中虚拟摇杆的使用EasyJoystick
    在屏幕拖拽3D物体移动
    LineRenderer组建实现激光效果
    unity3d对象池的使用
    自动寻路方案
    贪吃蛇方案
    unity3d射线控制移动
    文件压缩(读取文件优化)
    [LeetCode] 33. 搜索旋转排序数组 ☆☆☆(二分查找)
  • 原文地址:https://www.cnblogs.com/syscn/p/11622840.html
Copyright © 2011-2022 走看看