第一步:在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. kuma kong 团队开发的可视化&&安全的service mesh

    最近service mesh 的开源产品是越来越多了,好多团队都开源了自己的解决方案 maesh 最近kong 团队也开源了自己的service meshkuma 一张参考图 说明 kuma 没有基于 ...

  2. Python3 连接各类数据库

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口.它定义了一系列必须的对象和数据库存取方式, 以便为各种各样的底层数据库系统和多 ...

  3. 一篇JavaScript技术栈带你了解继承和原型链

    作者 | Jeskson 来源 | 达达前端小酒馆 1 在学习JavaScript中,我们知道它是一种灵活的语言,具有面向对象,函数式风格的编程模式,面向对象具有两点要记住,三大特性,六大原则. 那么 ...

  4. Spring和MyBatis整合(注解版)

    1.导入所需要的依赖 <!--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis& ...

  5. Missing Data Reconstruction in Remote Sensing Image With a Unified Spatial–Temporal–Spectral Deep Convolutional Neural Network(缺失数据补全,时空谱网络)

    摘要 文章针对修复坏波段(AQUA B6),恢复条带损失,恢复云污染提出了一个深度学习网络结构,他说 To date, to the best of our knowledge, no studies ...

  6. UE运行sas配置-WIN10

    1.在UE中配置SAS运行的工具: UE--高级---用户工具--工具配置 在命令行输入"D:\soft\SASHome\SASFoundation\9.4\sas.exe" -c ...

  7. Ubuntu 14.04下超级终端Minicom连接ARM(转)

    转自:https://blog.csdn.net/ajianyingxiaoqinghan/article/details/70209765 笔者的工作环境: PC系统:Ubuntu 14.04 LT ...

  8. Three.js 快速上手以及在 React 中运用[转]

    https://juejin.im/post/5ca22692f265da30a53d6656 github 的地址 欢迎 star! 之前项目中用到了 3D 模型演示的问题,整理了一下之前学习总结以 ...

  9. 【spring源码学习】spring的事务管理源码学习

    一.抽象概念 1.事务管理器 接口:org.springframework.transaction.PlatformTransactionManager 实现类:org.springframework ...

  10. 003 docker安装nginx

    一:安装与运行nginx 1.查找镜像网站 https://c.163yun.com/hub#/m/home/ 2.pull 3.查看当前在运行的容器 docker ps 4.启动nginx 使用后台 ...