第一步:在Spring配置中添加以下内容

<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver"

p:defaultEncoding="UTF-8" p:maxUploadSize="540000000" p:uploadTempDir="fileUpload/temp">

</bean>

第二步:编写上传Spring接口

/**

 * 上传专报

 * 

 * @param typeId

 * @param userId

 * @param file

 * @return

 */

@ResponseBody

@RequestMapping(params = "uploadDoc")

public Return uploadDoc(@RequestParam(value = "file", required = true) MultipartFile file,String newFileName) {

Return r = new Return();

System.out.println(newFileName);

try {

// 3.保存word文件

if (file != null) {

// 文件存入临时目录

String thisFile = SaveFile.save(file, newFileName);

if(thisFile!=null){

r.setE(1);

r.setS("上传专报成功!");

}else{

r.setE(0);

r.setS("上传专报失败!");

}

}

} catch (Exception e) {

e.printStackTrace();

r.setS("系统异常");

}

return r;

}

/**

 * 保存文件到临时目录

 * 

 * @param files

 * @param 保存文件路径包含文件名称

 * @return

 */

public static String save(MultipartFile files,String newFilePath) {

String fileName = files.getOriginalFilename();

if (fileName == null) {

return null;

}

File file = new File(newFilePath);

if(!file.exists()){

try {

FileOutputStream fop = new FileOutputStream(file);

file.createNewFile();

// 获取文件字节

byte[] contentInBytes = files.getBytes();

fop.write(contentInBytes);// 写入本地

fop.flush();

fop.close();

return newFilePath;

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

第三步:编写上传程序

上传程序所需jar:httpclient-4.5.3.jar,httpmime-4.5.3.jar

/**

 * 上传文件到指定地址URL

 *

 * @param 本地文件

 * @param 请求接口路径

 * @param newfilePath 其他参数

 * @return 是否成功

 */

public static boolean uploadFile(String filePath, String urlStr, String newfilePath) {

try {

String sTestsetFile = newfilePath;

String sURL = urlStr;//"http://localhost:8080/qxfw/productController.do?uploadDoc";

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost uploadFile = new HttpPost(sURL);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

builder.addTextBody("newFileName", newfilePath, ContentType.TEXT_PLAIN);

// 把文件加到HTTP的post请求中

File f = new File(sTestsetFile);

builder.addBinaryBody("file", new FileInputStream(f), ContentType.APPLICATION_OCTET_STREAM, f.getName());

HttpEntity multipart = builder.build();

uploadFile.setEntity(multipart);

CloseableHttpResponse response = httpClient.execute(uploadFile);

HttpEntity responseEntity = response.getEntity();

String sResponse = EntityUtils.toString(responseEntity, "UTF-8");

if (sResponse.contains("成功")) {

return true;

} else {

return false;

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return false;

}

java 调用Spring接口上传文件及其他参数填充的更多相关文章

  1. Spring MVC上传文件原理和resolveLazily说明

    问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...

  2. Spring MVC上传文件

    Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...

  3. springboot(十七):使用Spring Boot上传文件

    上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...

  4. (转)Spring Boot(十七):使用 Spring Boot 上传文件

    http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...

  5. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  6. 使用Spring Boot上传文件

    原文:http://www.cnblogs.com/ityouknow/p/8298344.html 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spri ...

  7. Spring Boot(十七):使用 Spring Boot 上传文件

      上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 ...

  8. Spring MVC 上传文件

    Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data"  input的typ ...

  9. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

随机推荐

  1. [React] Write a generic React Suspense Resource factory

    Using Suspense within our component isn't exactly ergonomic. Let's put all that logic into a reusabl ...

  2. 腾讯蓝鲸cmdb部署

    蓝鲸配置平台 (CMDB)http://172.16.6.10:8088 环境(单机测试): Centos6 16G 200G 依赖环境: Java 1.8.0_92 python 2.7 ZooKe ...

  3. 代码注入/文件包含 弹出Meterpreter

    主要通过 msf 中 exploit 的 web_delivery 模块来实现此功能 0x01 前提背景 目标设备存在远程文件包含漏洞或者命令注入漏洞,想在目标设备上加载webshell,但不想在目标 ...

  4. bzoj4868 期末考试 题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4868 显然我们只关注最后出分的学科. 刚开始想的是dp,然而不知道如何记录状态. 突然就想到了正解 ...

  5. 分布式系统 与 Google

    google 论文 http://duanple.com/?p=170 google 论文与开源 http://duanple.com/?p=1096 分布式系统论文集 https://github. ...

  6. Windows 文件过滤驱动经验总结

    Windows 文件过滤驱动经验总结作者:sinister 本文转载自驱动开发网 看了 ChuKuangRen 的第二版<文件过滤驱动开发教程>后,颇有感触.我想,交流都是建立在平等的基础 ...

  7. 个人收集的Android开源项目

    1. KnowWeather 下载:GitHub 一款 Android 开源天气 App ,包含天气信息.详情.生活指数等,通知栏,桌面小部件,定时更新天气等等,应用没有任何广告,支持县级.区级城市的 ...

  8. openwrt的shell下如何访问寄存器的内容?

    答:通过devmem工具(在openwrt的make menuconfig中可以使能该工具) $ busybox devmem 0x123456

  9. 解决:File "/usr/lib/python2.7/site-packages/more_itertools/more.py", line 340 def _collate(*iterables, key=lambda a: a, reverse=False): 的报错

    cyberb commented on 15 Apr Traceback (most recent call last): File "/snap/users/x1/python/bin/l ...

  10. Wordpress 安装或切换不同的版本

    如果升级到最新版本的 Wordpress 后,发现有 bug,需要回滚回上一个相对稳定的版本,可以按照如下步骤: 一.到官网下载压缩包 https://wordpress.org/download/r ...