java 调用Spring接口上传文件及其他参数填充
第一步:在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接口上传文件及其他参数填充的更多相关文章
- Spring MVC上传文件原理和resolveLazily说明
问题:使用Spring MVC上传大文件,发现从页面提交,到进入后台controller,时间很长.怀疑是文件上传完成后,才进入.由于在HTTP首部自定义了“Token”字段用于权限校验,Token的 ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- springboot(十七):使用Spring Boot上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spring Boot上传文件的小案例. 1.pom包配置 我们使用Spring Boot最新版本1.5.9. ...
- (转)Spring Boot(十七):使用 Spring Boot 上传文件
http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...
- Spring Boot(十七):使用Spring Boot上传文件
Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...
- 使用Spring Boot上传文件
原文:http://www.cnblogs.com/ityouknow/p/8298344.html 上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个Spri ...
- Spring Boot(十七):使用 Spring Boot 上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 ...
- Spring MVC 上传文件
Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data" input的typ ...
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...
随机推荐
- 【JVM】内存和SWAP问题
一.现象 1.系统稳定运行,偶尔发生响应超时的情况.查看下游依赖服务和数据库状态都良好.超时完全是由于服务本身问题造成的.重启不能解决问题,一直会间隔性的发生超时 二.原因分析 第一种情况,系统内存够 ...
- pipelinewise 学习一 docker方式安装
pipelinewise 没有提供基于pip 包的模式进行安装,而且推荐使用docker 以及源码的方式 以下是一个使用docker运行的方式 安装 git clone https://githu ...
- Problem 1 珠江夜游 (cruise .cpp)———2019.10.6
Problem 1 珠江夜游 (cruise.cpp)[题目描述]小 Z 放假后难得来一趟广州游玩,当然要吃遍广州各路美食小吃然后再到珠江新城看看远近闻名的小蛮腰啦!可当小 Z 一路吃吃吃以后,天渐渐 ...
- C++中vector的使用总结
vector简单说明 vector也是一个容器,并且是个顺序容器.顺序容器有可变长数组vector.双向链表list.双端队列deque. 顺序容器的定义,是因为容器元素的位置和他们的值大小无关,也就 ...
- nginx 日志之 access_log
web服务器的访问日志是非常重要的,我们可以通过访问日志来分析用户的访问情况, 也可以通过访问日志发现一些异常访问,比如cc攻击. 格式: access_log /path/to/logfile fo ...
- Guava Cache 参数配置说明
系统中用到了了Guava Cache: private DriverInfoServiceImpl(DriverClientProxy driverClientProxy) { this.driver ...
- What is the difference between UNION and UNION ALL?
What is the difference between UNION and UNION ALL? UNION removes duplicate records (where all colum ...
- 变异的功能 | variants function | coding | non-coding
variant主要分为两类: coding noncoding 这两大类的分析方法截然不同,coding主要直接影响了mRNA和蛋白:而noncoding,主要是影响调控. coding编码区如何分析 ...
- 【maven】命令
一.maven打包命令 mvn打包,并指定 Profiles配置文件 和 模块名 mvn clean package {-P prod} -pl {groupId}:{artifactId} -am ...
- MAC OS系统替换homebrew使用阿里云或中科大的镜像源
阿里云镜像 # 替换brew.git: cd "$(brew --repo)" git remote set-url origin https://mirrors.aliyun.c ...