必威体育Betway必威体育官网
当前位置:首页 > IT技术

java 文件上传demo

时间:2019-07-13 16:43:25来源:IT技术作者:seo实验室小编阅读:83次「手机版」
 

stanlee

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

基本pom引用:

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
        </dependency>

以下是实现方法:

public static String doPost(String url, Map<String, String> 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<String, String> 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
            }
        }
    }

相关阅读

Java技术对网站建设有什么好处

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

JavaScript主要作用是什么呢

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

java.lang.UnsupportedClassVersionError

截图:                                                            错误日志: Exception in thr

Java之反射——类对象

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

Java 混淆器(obfuscate)

Java 混淆器就是给.class加密以防止反编译的工具 开源的  RetroGuard   http://www.retrologic.com/ IBM的  JAX    

分享到:

栏目导航

推荐阅读

热门阅读