博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中模拟POST上传文件
阅读量:4676 次
发布时间:2019-06-09

本文共 5365 字,大约阅读时间需要 17 分钟。

/**     *     * @param url 请求URL     * @param filePath 本地文件地址     * @return     */    public static String upload(String url,String filePath){        String fdfsPath = "";        try {            HttpClient httpclient = new DefaultHttpClient();            HttpPost httppost = new HttpPost(url);            File file = new File(filePath);            String name = file.getName();            InputStream in = new FileInputStream(file);            MultipartEntity reqEntity = new MultipartEntity();            InputStreamBody inputStreamBody = new InputStreamBody(in,name);            StringBody fileNam = new StringBody(name);            StringBody dateFlag = new StringBody("20160122152301");            StringBody datumType = new StringBody("0");            StringBody uploadWay = new StringBody("0");            StringBody userId = new StringBody("0538");            StringBody tenderId = new StringBody("2315");            StringBody metrialsType = new StringBody("25");            StringBody ip = new StringBody("0.0.0.1");            StringBody driverName = new StringBody("huawei");            StringBody systemVersion = new StringBody("djf");            StringBody position = new StringBody("信息路38",  Charset.forName("utf8"));      //文件流             reqEntity.addPart("datums", inputStreamBody);            reqEntity.addPart("fileName", fileNam);            reqEntity.addPart("dateFlag", dateFlag);            reqEntity.addPart("datumType", datumType);            reqEntity.addPart("uploadWay", uploadWay);            reqEntity.addPart("userId", userId);            reqEntity.addPart("tenderId", tenderId);            reqEntity.addPart("metrialsType", metrialsType);            reqEntity.addPart("ip", ip);            reqEntity.addPart("driverName", driverName);            reqEntity.addPart("systemVersion", systemVersion);            reqEntity.addPart("position", position);            httppost.setEntity(reqEntity);            HttpResponse response = httpclient.execute(httppost);            int statusCode = response.getStatusLine().getStatusCode();            if(statusCode == HttpStatus.SC_OK){                System.out.println("服务器正常响应.....");                HttpEntity resEntity = response.getEntity();                System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据                System.out.println(resEntity.getContent());                EntityUtils.consume(resEntity);            }        } catch (Exception e) {            e.printStackTrace();        }        return "";    }    /**     * @param args     */    public static void main(String[] args) {        upload("http://192.168.1.1:8080/xxxImageUpload.action","E:\\weatertest\\002.jpg");    }

 图片下载

  

private static void downFile() {        try {            String path = "E:\\downurl\\2016022302\\";            File downFileUrl = new File(path);            File[] files = downFileUrl.listFiles();            for (File file:files) {                BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));                String downParas = null;                while((downParas=bfr.readLine())!=null){                    System.out.println("下载参数:"+downParas);                    String[] dows = downParas.split("&");                    if(dows==null||dows.length<3){                        System.out.println("数据不正常downParas:"+downParas);                    }else{                        String tenderFlod = path+dows[0].trim();                        File tender_fold = new File(tenderFlod.trim());                        if(!tender_fold.exists()){                            System.out.println("创建文件夹:"+tenderFlod.trim());                            tender_fold.mkdir();                        }                        String leiFold = tenderFlod+"\\"+dows[1].trim();                        File lei_Fold = new File(leiFold);                        if(!lei_Fold.exists()){                            System.out.println("创建文件夹:"+leiFold);                            lei_Fold.mkdir();                        }                        HttpClient httpclient = new DefaultHttpClient();                        HttpPost httppost = new HttpPost("http://imagelocal.eloancn.com/xxxdownImg.action");                        StringBody fileName = new StringBody(dows[2]);                        MultipartEntity reqEntity = new MultipartEntity();                        reqEntity.addPart("imgPath", fileName);//fileName文件名称                        httppost.setEntity(reqEntity);                        HttpResponse response = httpclient.execute(httppost);                        int statusCode = response.getStatusLine().getStatusCode();                        if(statusCode == HttpStatus.SC_OK){                            System.out.println("服务器正常响应....."+dows[2].substring(dows[2].lastIndexOf("/")+1)+"下载完成。");                            HttpEntity resEntity = response.getEntity();                            String savepath = lei_Fold+"//"+dows[2].substring(dows[2].lastIndexOf("/")+1).trim();                            FileOutputStream fos = new FileOutputStream(new File(savepath));                            resEntity.writeTo(fos);                        }                    }                }            }        } catch (Exception e) {            e.printStackTrace();        }    }

 

转载于:https://www.cnblogs.com/sagech/p/5671505.html

你可能感兴趣的文章
poj1015【DP.......无奈了】
查看>>
C#性能优化的一些技巧
查看>>
PAT 甲级 1024 Palindromic Number
查看>>
信息安全经典书籍
查看>>
ios坐标位置转换
查看>>
Java多线程—JUC原子类
查看>>
C#中常用到的时间函数(天数差、星期几等)
查看>>
如何理解一台服务器可以绑定多个ip,一个ip可以绑定多个域名
查看>>
改进delphi中的RoundTo函数
查看>>
Microsoft Visual SourceSafe使用经验
查看>>
威尔逊定理及证明
查看>>
[LeetCode] Peeking Iterator
查看>>
Understanding Unix/Linux Programming-用户程序play_again4.c
查看>>
算法总结
查看>>
WPF中使用USERCONTROL
查看>>
图片,base64 互转
查看>>
ES6 有什么新东西
查看>>
cache—主存—辅存三级调度模拟
查看>>
Java线程的定义
查看>>
UglifyJS 压缩选项
查看>>