java 上传下载demo_「stanlee」java 文件上传demo - seo实验室

stanlee

每次写代码都要查找,所以就放这里面备份了

基本pom引用:

org.apache.httpcomponents

httpclient

4.5.2

org.apache.httpcomponents

httpmime

4.5.2

以下是实现方法:

public static String doPost(String url, Map paramMap, String filePath, String fileName){

if (StringUtils.isempty(url) || StringUtils.isEmpty(filePath) ) {

throw new illegalargumentException("params ERROR!");

}

HttpPost httpPost = new HttpPost(url);

CloseableHttpClient httpClient = HttpClients.createDefault();

try {

MultipartEntitybuilder builder = MultipartEntityBuilder.create()

.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)

.setCharset(consts.UTF_8);

FileBody fileBody;

File file = new File(filePath);

if (StringUtils.isNotEmpty(fileName)) {

fileBody = new FileBody(file, contentType.DEFAULT_binary, fileName);

} else {

fileBody = new FileBody(file);

}

builder.addPart("file", fileBody);

if (MapUtils.isNotEmpty(paramMap)) {

for (Map.Entry entry : paramMap.entrySet()) {

builder.addPart(entry.getKey()

, new StringBody(entry.getValue()

, ContentType.create("text/plain", Consts.UTF_8)));

}

}

String result;

httpPost.setEntity(builder.build());

CloseableHttpresponse response = httpClient.execute(httpPost);

try {

StatusLine statusLine = response.getStatusLine();

if (statusLine.getStatusCode() != HttpStatus.SC_OK) {

throw new runtimeexception("unexpected failure: " + statusLine.toString());

}

HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

result = EntityUtils.toString(resEntity, Charset.forName("UTF-8"));

EntityUtils.consume(resEntity);

} else {

result = null;

}

} finally {

response.close();

}

return result;

} catch (Exception e) {

throw new RuntimeException("Exception occurred when send post request[url:" + url, e);

} finally {

try {

httpClient.close();

} catch (Exception e) {

//np

}

}

}

相关阅读

工程师为消费类设备开发了这种语言,并使其与当时适度的CPU兼容时保持了简单性。从那时起,这种面向对象的语言已用于创建简单到

JavaScript主要作用是什么呢?学习编程的同学对JavaScript并不陌生,JavaScript是前端技术中非常重要的内容,是网站搭建必不可少的

截图:

错误日志:

Exception in thr

学了一段时间Java了,但是还是很菜,看到反射这一节的时候,就有点厌烦,看不下去了,过了一段时间后我又翻了回来,因为要学习后面的,所以反射

Java 混淆器就是给.class加密以防止反编译的工具

开源的  RetroGuard   http://www.retrologic.com/

IBM的  JAX

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值