java模拟从http上下载文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java模拟从http上下载文件相关的知识,希望对你有一定的参考价值。
1.依赖
Apache httpclient 包。
2.代码
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://upload.eloancn.com/downLoadImg.action"); StringBody fileName = new StringBody(url);//图片路径 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("服务器正常响应.....下载完成。"); HttpEntity resEntity = response.getEntity(); String savepath = lei_Fold+"//"+dows[2].substring(dows[2].lastIndexOf("/")+1);//图片保存路径 FileOutputStream fos = new FileOutputStream(new File(savepath)); resEntity.writeTo(fos); }
3.解析
见源码注释
以上是关于java模拟从http上下载文件的主要内容,如果未能解决你的问题,请参考以下文章