第一步:在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. Ansible自动部署tomcat

    1.首先准备3台机器 ansible机器:192.168.52.34 目标主机:192.168.52.35 目标主机:192.168.52.36 2.关闭防火墙 [root@localhost ~]# ...

  2. Redis 迁移 DB; move key db

    redis 移动 DB MOVE key db将当前数据库的 key 移动到给定的数据库 db 当中.如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定 key ,或者 key 不存 ...

  3. 8-网页,网站,微信公众号基础入门(使用Python程序实现微信token验证)

    https://www.cnblogs.com/yangfengwu/p/11062034.html 设置一下IP白名单:   填写自己的服务器的IP地址 是不是不可以哈,有多少人都会入这个坑.... ...

  4. [图形计算器]Desmos

    一.图形计算器 var elt = document.getElementById('calculator'); var calculator = Desmos.GraphingCalculator( ...

  5. GoCN每日新闻(2019-11-04)

    GoCN每日新闻(2019-11-04) GoCN每日新闻(2019-11-04)   1. Go中垃圾收集器是如何标记内存的 https://medium.com/a-journey-with-go ...

  6. 2018-2019-2 20175211 实验四《Android程序设计》实验报告

    目录 一.实验内容及步骤 1.Android Studio的安装测试 2.Activity测试 3.UI测试 4.布局测试 5.事件处理测试 二.问题及解决方法 三.代码托管 四.实验心得体会 一.实 ...

  7. [Web] HTML5新特性history pushState/replaceState解决浏览器刷新缓存

    转载: https://www.jianshu.com/p/cf63a1fabc86 现实开发中,例如‘商品列表页’跳转‘商品详情页’,返回时,不重新加载刷新页面,并且滚动到原来的位置. 1.首先,先 ...

  8. CentOS7下将Python的版本升级为3.7

    众所周知,在2020年python官方将不再支持2.7版本的python,所以使用3.x版本的python是必要的,但是linux中默认安装的python一般是2.6和2.7版本的,要是使用linux ...

  9. android细节之android.intent.category.DEFAULT的使用

    我们知道,实现android的Activity之间相互跳转需要用到Intent, Intent又分为显式Intent和隐式Intent, 显式Intent很简单,比如我在FirstActivity中想 ...

  10. layer.confirm 防止post重复提交

    layer.confirm("确定对该资源单的价格"+isa+value+"元吗!", function(index, layero){ $(layero).f ...