Java中模拟POST上传文件



/**
* * @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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值