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