SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收。

将文件的字节流,放入ByteArrayResource中,并重写getFilename方法。

然后将ByteArrayResource放入MultiValueMap中(如果是Feign调用,方法里传参就是MultiValueMap),

然后进行上传时,Spring会自动识别到Map中的文件数据,然后通过FormHttpMessageConverter,将数据转成form表单型的multipart/formdata请求。

这里有个坑!

Spring web 4里面的FormHttpMessageConverter在将文件转成formdata时,会将文件名转成Byte[],但是使用的编码却是写死 US-ASCII,该编码不支持中文,使用该编码转换后,中文变成?号,是无法转回来的。

我想到的解决方法:

1.将spring版本升到5,Spring5里面,该编码是可以传入修改的。Springboot,默认UTF8

2.客户端进行一次编码,比如URLEncoder。然后服务端进行Decoder。

贴部分代码:

Feign

调用方,使用Spring 的MultiValueMap类,将文件File 转成 Resource,如果多个文件,则可以循环 用 add 方法,放入一个key下。

MultiValueMap是允许一key多值的。

或者,将多个Resource放入list,然后将list  put 进 map中。

接收方

接收,可以用

(MultiValueMap map)

如果有其他的 值。

则是(MultiValueMap map,String XXX,String  AAA)

多文件,则是

(MultiValueMap[] map,String XXX,String  AAA)

或者用对象接收,也可以,不需要 @RequestBody 注解,这个注解是接收 http body里的json的。

(Bean bean),bean对象里,则是  MultiValueMap[] map,String XXX,String  AAA

======================  分割线  =================================

我在查询Feign上传文件时,还查到了另一种方式,就是专门给Feign方式提供的feign form相关Jar包,

引入Jar包后,然后进行相关配置,便可以在Feign方法中,参数直接传递MultipartFile。

该方法,或许也可以解决Spring4的编码问题。

===================     分隔线:2018-11-8补充 关于 feign form用法      ====================

先引入相关jar:

  1. <dependency>
  2. <groupId>io.github.openfeign.form</groupId>
  3. <artifactId>feign-form</artifactId>
  4. <version>3.2.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.github.openfeign.form</groupId>
  8. <artifactId>feign-form-spring</artifactId>
  9. <version>3.2.2</version>
  10. </dependency>

  

  1. @Bean
  2. public Logger.Level feignLoggerLevel() {
  3. return Logger.Level.FULL;
  4. }
  5.  
  6. @Bean
  7. public Encoder feignFormEncoder() {
  8. return new SpringFormEncoder();
  9. }

  

feign 调用方法 写法 :

  1. save(@RequestPart MultipartFile file,@RequestParam("khbh") String khbh)
  2.  
  3. 但是如果,参数多时,一个一个写较为麻烦,可以用
  1. save(Map<String,?> param)
  2.  
  3. 但是,经过测试,发现如果 mapvaluenull,会出现异常。(原因好像是因为,在将 值写入 formdata时,没有null判断)
  4.  
  5. ========== 上面的用的 feign form
  6.  
  7. 下面 有从网络上查到的,是类似于 feign form的解决方式:
    http://b-l-east.iteye.com/blog/2373462
  8.  
  1. 糞坑-SpringCloud中使用Feign的坑
  2. 示例如下:
  3. @FeignClient("service-resource")
  4. //@RequestMapping("/api/test")
  5. public interface TestResourceItg {
  6.  
  7. @RequestMapping(value = "/api/test/raw", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
  8. public String raw1(@PathVariable("subject") String subject, // 标题
  9. @RequestParam("content") String content); // 内容
  10.  
  11. }
  12.  
  13. 说明:
  14. *使用RequestMapping中的consumes指定生成的请求的Content-Type
  15. *RequestParam指定的参数会拼接在URL之后,如: ?name=xxx&age=18
  16. *PathVariable指定的参数会放到一个LinkedHashMap<String, ?>传入到feignEncoder中进行处理,而在Spring中实现了该接口的EncoderSpringEncoder,而该实现又会使用Spring中的HttpMessageConverter进行请求体的写入。
  17.  
  18. 坑:
  19. *不要在接口类名上使用RequestMapping,虽然可以使用,但同时SpringMVC会把该接口的实例当作Controller开放出去,这个可以在启动的Mapping日志中查看到
  20. *使用默认的SpringEncoder,在不指定consumes时,PathVariable中的参数会生成JSON字符串发送,且默认情况下不支持Form表单的生成方式,原因为:FormHttpMessageConverter只能处理MultiValueMap,而使用PathVariable参数被放在了HashMap中。默认更不支持文件上传。其实已经有支持处理各种情况的HttpMessageConverter存在。
  21.  
  22. 填坑:
  23. *支持Form表单提交:只需要编写一个支持MapFormHttpMessageConverter即可,内部可调用FormHttpMessageConverter的方法简化操作。
  24. *支持文件上传:只需要把要上传的文件封装成一个Resource(该Resource一定要实现filename接口,这个是把请求参数解析成文件的标识),使用默认的ResourceHttpMessageConverter处理即可。
  25. *支持处理MultipartFile参数:编写一个支持MultipartFileMultipartFileHttpMessageConverter即可,内部可调用ResourceHttpMessageConverter实现,同时注意需要将其添加至FormHttpMessageConverterParts中,并重写FormHttpMessageConvertergetFilename方法支持从MultipartFile中获取filename
  26. *所有的HttpMessageConverter直接以@Bean的方式生成即可,spring会自动识别添加
  27.  
  28. 完美支持表单和文件上传:
  29. 方案一:
  30. 使用附件中的MapFormHttpMessageConverter.javaMultipartFileHttpMessageConverter.java
  31. Spring中进行如下配置即可
  32. @Bean
  33. public MapFormHttpMessageConverter mapFormHttpMessageConverter(MultipartFileHttpMessageConverter multipartFileHttpMessageConverter) {
  34. MapFormHttpMessageConverter mapFormHttpMessageConverter = new MapFormHttpMessageConverter();
  35. mapFormHttpMessageConverter.addPartConverter(multipartFileHttpMessageConverter);
  36. return mapFormHttpMessageConverter;
  37. }
  38.  
  39. @Bean
  40. public MultipartFileHttpMessageConverter multipartFileHttpMessageConverter() {
  41. return new MultipartFileHttpMessageConverter();
  42. }
  43. 方案二:
  44. 使用FeignSpringFormEncoder.java
  45. Spring中配置如下:
  46. @Bean
  47. public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
  48. return new FeignSpringFormEncoder(messageConverters);
  49. }
  50.  
  51. 推荐使用方案一
  52. 方案二为参考https://github.com/pcan/feign-client-test而来,未测

  

上面方案中所用代码,贴在下面:

  1. package com.access.service.saas.cmpt.utl;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8.  
  9. import org.springframework.core.io.InputStreamResource;
  10. import org.springframework.http.HttpInputMessage;
  11. import org.springframework.http.HttpOutputMessage;
  12. import org.springframework.http.MediaType;
  13. import org.springframework.http.converter.HttpMessageConverter;
  14. import org.springframework.http.converter.HttpMessageNotReadableException;
  15. import org.springframework.http.converter.HttpMessageNotWritableException;
  16. import org.springframework.http.converter.ResourceHttpMessageConverter;
  17. import org.springframework.web.multipart.MultipartFile;
  18.  
  19. /**
  20. * @author elvis.xu
  21. * @since 2017-05-09 11:17
  22. */
  23. public class MultipartFileHttpMessageConverter implements HttpMessageConverter<MultipartFile> {
  24. protected List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
  25. protected ResourceHttpMessageConverter resourceHttpMessageConverter;
  26.  
  27. public MultipartFileHttpMessageConverter() {
  28. supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
  29. resourceHttpMessageConverter = new ResourceHttpMessageConverter();
  30. }
  31.  
  32. public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
  33. this.supportedMediaTypes = supportedMediaTypes;
  34. }
  35.  
  36. @Override
  37. public List<MediaType> getSupportedMediaTypes() {
  38. return Collections.unmodifiableList(this.supportedMediaTypes);
  39. }
  40.  
  41. @Override
  42. public boolean canRead(Class<?> clazz, MediaType mediaType) {
  43. return false;
  44. }
  45.  
  46. @Override
  47. public boolean canWrite(Class<?> clazz, MediaType mediaType) {
  48. if (!MultipartFile.class.isAssignableFrom(clazz)) {
  49. return false;
  50. }
  51. if (mediaType == null || MediaType.ALL.equals(mediaType)) {
  52. return true;
  53. }
  54. for (MediaType supportedMT : getSupportedMediaTypes()) {
  55. if (supportedMT.isCompatibleWith(mediaType)) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61.  
  62. @Override
  63. public MultipartFile read(Class<? extends MultipartFile> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
  64. return null;
  65. }
  66.  
  67. @Override
  68. public void write(MultipartFile file, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
  69. MultipartFileResource multipartFileResource = new MultipartFileResource(file);
  70. resourceHttpMessageConverter.write(multipartFileResource, contentType, outputMessage);
  71. }
  72.  
  73. public static class MultipartFileResource extends InputStreamResource {
  74.  
  75. private final String filename;
  76. private final long size;
  77.  
  78. public MultipartFileResource(MultipartFile multipartFile) throws IOException {
  79. this(multipartFile.getOriginalFilename(), multipartFile.getSize(), multipartFile.getInputStream());
  80. }
  81.  
  82. public MultipartFileResource(String filename, long size, InputStream inputStream) {
  83. super(inputStream);
  84. this.size = size;
  85. this.filename = filename;
  86. }
  87.  
  88. @Override
  89. public String getFilename() {
  90. return this.filename;
  91. }
  92.  
  93. @Override
  94. public InputStream getInputStream() throws IOException, IllegalStateException {
  95. return super.getInputStream(); //To change body of generated methods, choose Tools | Templates.
  96. }
  97.  
  98. @Override
  99. public long contentLength() throws IOException {
  100. return size;
  101. }
  102.  
  103. }
  104. }

  

  1. package com.access.service.saas.cmpt.utl;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.lang.reflect.Type;
  8. import java.nio.charset.Charset;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import feign.RequestTemplate;
  14. import feign.codec.EncodeException;
  15.  
  16. import org.springframework.beans.factory.ObjectFactory;
  17. import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
  18. import org.springframework.cloud.netflix.feign.support.SpringEncoder;
  19. import org.springframework.core.io.InputStreamResource;
  20. import org.springframework.core.io.Resource;
  21. import org.springframework.http.HttpEntity;
  22. import org.springframework.http.HttpHeaders;
  23. import org.springframework.http.HttpOutputMessage;
  24. import org.springframework.http.MediaType;
  25. import org.springframework.http.converter.HttpMessageConverter;
  26. import org.springframework.util.LinkedMultiValueMap;
  27. import org.springframework.web.multipart.MultipartFile;
  28.  
  29. /**
  30. * @author elvis.xu
  31. * @since 2017-04-11 15:33
  32. */
  33. public class FeignSpringFormEncoder extends SpringEncoder {
  34.  
  35. protected ObjectFactory<HttpMessageConverters> messageConverters;
  36. protected HttpHeaders multipartHeaders = new HttpHeaders();
  37. public static final Charset UTF_8 = Charset.forName("UTF-8");
  38.  
  39. public FeignSpringFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
  40. super(messageConverters);
  41. this.messageConverters = messageConverters;
  42. multipartHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
  43. }
  44.  
  45. protected static boolean isFormRequest(Type type) {
  46. return MAP_STRING_WILDCARD.equals(type);
  47. }
  48.  
  49. protected static boolean isMultipart(Object body, Type bodyType) {
  50. if (isFormRequest(bodyType)) {
  51. Map<String, ?> map = (Map<String, ?>) body;
  52. for (Map.Entry<String, ?> entry : map.entrySet()) {
  53. Object value = entry.getValue();
  54. if (isMultipartFile(value) || isMultipartFileArray(value)) {
  55. return true;
  56. }
  57. }
  58. }
  59. return false;
  60. }
  61.  
  62. protected static boolean isMultipartFile(Object obj) {
  63. return obj instanceof MultipartFile;
  64. }
  65.  
  66. protected static boolean isMultipartFileArray(Object o) {
  67. return o != null && o.getClass().isArray() && MultipartFile.class.isAssignableFrom(o.getClass().getComponentType());
  68. }
  69.  
  70. @Override
  71. public void encode(Object requestBody, Type bodyType, RequestTemplate request) throws EncodeException {
  72. if (isMultipart(requestBody, bodyType)) {
  73. encodeMultipartFormRequest((Map<String, ?>) requestBody, request);
  74. } else {
  75. super.encode(requestBody, bodyType, request);
  76. }
  77. }
  78.  
  79. /**
  80. * Encodes the request as a multipart form. It can detect a single {@link MultipartFile}, an
  81. * array of {@link MultipartFile}s, or POJOs (that are converted to JSON).
  82. *
  83. * @param formMap
  84. * @param template
  85. * @throws EncodeException
  86. */
  87. private void encodeMultipartFormRequest(Map<String, ?> formMap, RequestTemplate template) throws EncodeException {
  88. if (formMap == null) {
  89. throw new EncodeException("Cannot encode request with null form.");
  90. }
  91. LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
  92. for (Map.Entry<String, ?> entry : formMap.entrySet()) {
  93. Object value = entry.getValue();
  94. if (isMultipartFile(value)) {
  95. map.add(entry.getKey(), encodeMultipartFile((MultipartFile) value));
  96. } else if (isMultipartFileArray(value)) {
  97. encodeMultipartFiles(map, entry.getKey(), Arrays.asList((MultipartFile[]) value));
  98. } else {
  99. map.add(entry.getKey(), encodeJsonObject(value));
  100. }
  101. }
  102. encodeRequest(map, multipartHeaders, template);
  103. }
  104.  
  105. /**
  106. * Wraps a single {@link MultipartFile} into a {@link HttpEntity} and sets the
  107. * {@code Content-type} header to {@code application/octet-stream}
  108. *
  109. * @param file
  110. * @return
  111. */
  112. private HttpEntity<?> encodeMultipartFile(MultipartFile file) {
  113. HttpHeaders filePartHeaders = new HttpHeaders();
  114. filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  115. try {
  116. Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream());
  117. return new HttpEntity<>(multipartFileResource, filePartHeaders);
  118. } catch (IOException ex) {
  119. throw new EncodeException("Cannot encode request.", ex);
  120. }
  121. }
  122.  
  123. /**
  124. * Fills the request map with {@link HttpEntity}s containing the given {@link MultipartFile}s.
  125. * Sets the {@code Content-type} header to {@code application/octet-stream} for each file.
  126. *
  127. * @param map the current request map.
  128. * @param name the name of the array field in the multipart form.
  129. * @param files
  130. */
  131. private void encodeMultipartFiles(LinkedMultiValueMap<String, Object> map, String name, List<? extends MultipartFile> files) {
  132. HttpHeaders filePartHeaders = new HttpHeaders();
  133. filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  134. try {
  135. for (MultipartFile file : files) {
  136. Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream());
  137. map.add(name, new HttpEntity<>(multipartFileResource, filePartHeaders));
  138. }
  139. } catch (IOException ex) {
  140. throw new EncodeException("Cannot encode request.", ex);
  141. }
  142. }
  143.  
  144. /**
  145. * Wraps an object into a {@link HttpEntity} and sets the {@code Content-type} header to
  146. * {@code application/json}
  147. *
  148. * @param o
  149. * @return
  150. */
  151. private HttpEntity<?> encodeJsonObject(Object o) {
  152. HttpHeaders jsonPartHeaders = new HttpHeaders();
  153. jsonPartHeaders.setContentType(MediaType.APPLICATION_JSON);
  154. return new HttpEntity<>(o, jsonPartHeaders);
  155. }
  156.  
  157. /**
  158. * Calls the conversion chain actually used by
  159. * {@link org.springframework.web.client.RestTemplate}, filling the body of the request
  160. * template.
  161. *
  162. * @param value
  163. * @param requestHeaders
  164. * @param template
  165. * @throws EncodeException
  166. */
  167. private void encodeRequest(Object value, HttpHeaders requestHeaders, RequestTemplate template) throws EncodeException {
  168. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  169. HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
  170. try {
  171. Class<?> requestType = value.getClass();
  172. MediaType requestContentType = requestHeaders.getContentType();
  173. for (HttpMessageConverter<?> messageConverter : messageConverters.getObject().getConverters()) {
  174. if (messageConverter.canWrite(requestType, requestContentType)) {
  175. ((HttpMessageConverter<Object>) messageConverter).write(value, requestContentType, dummyRequest);
  176. break;
  177. }
  178. }
  179. } catch (IOException ex) {
  180. throw new EncodeException("Cannot encode request.", ex);
  181. }
  182. HttpHeaders headers = dummyRequest.getHeaders();
  183. if (headers != null) {
  184. for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
  185. template.header(entry.getKey(), entry.getValue());
  186. }
  187. }
  188. /*
  189. we should use a template output stream... this will cause issues if files are too big,
  190. since the whole request will be in memory.
  191. */
  192. template.body(outputStream.toByteArray(), UTF_8);
  193. }
  194.  
  195. /**
  196. * Dummy resource class. Wraps file content and its original name.
  197. */
  198. static class MultipartFileResource extends InputStreamResource {
  199.  
  200. private final String filename;
  201. private final long size;
  202.  
  203. public MultipartFileResource(String filename, long size, InputStream inputStream) {
  204. super(inputStream);
  205. this.size = size;
  206. this.filename = filename;
  207. }
  208.  
  209. @Override
  210. public String getFilename() {
  211. return this.filename;
  212. }
  213.  
  214. @Override
  215. public InputStream getInputStream() throws IOException, IllegalStateException {
  216. return super.getInputStream(); //To change body of generated methods, choose Tools | Templates.
  217. }
  218.  
  219. @Override
  220. public long contentLength() throws IOException {
  221. return size;
  222. }
  223.  
  224. }
  225.  
  226. /**
  227. * Minimal implementation of {@link org.springframework.http.HttpOutputMessage}. It's needed to
  228. * provide the request body output stream to
  229. * {@link org.springframework.http.converter.HttpMessageConverter}s
  230. */
  231. private class HttpOutputMessageImpl implements HttpOutputMessage {
  232.  
  233. private final OutputStream body;
  234. private final HttpHeaders headers;
  235.  
  236. public HttpOutputMessageImpl(OutputStream body, HttpHeaders headers) {
  237. this.body = body;
  238. this.headers = headers;
  239. }
  240.  
  241. @Override
  242. public OutputStream getBody() throws IOException {
  243. return body;
  244. }
  245.  
  246. @Override
  247. public HttpHeaders getHeaders() {
  248. return headers;
  249. }
  250.  
  251. }
  252. }

  

  1. package com.access.service.saas.cmpt.utl;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import org.springframework.http.HttpInputMessage;
  8. import org.springframework.http.HttpOutputMessage;
  9. import org.springframework.http.MediaType;
  10. import org.springframework.http.converter.FormHttpMessageConverter;
  11. import org.springframework.http.converter.HttpMessageConverter;
  12. import org.springframework.http.converter.HttpMessageNotReadableException;
  13. import org.springframework.http.converter.HttpMessageNotWritableException;
  14. import org.springframework.util.LinkedMultiValueMap;
  15. import org.springframework.util.MultiValueMap;
  16. import org.springframework.web.multipart.MultipartFile;
  17.  
  18. /**
  19. * @author elvis.xu
  20. * @since 2017-05-09 10:58
  21. */
  22. public class MapFormHttpMessageConverter implements HttpMessageConverter<Map<String, ?>> {
  23.  
  24. protected FormHttpMessageConverter formHttpMessageConverter;
  25.  
  26. public MapFormHttpMessageConverter() {
  27. this.formHttpMessageConverter = new MultipartFormHttpMessageConverter();
  28. }
  29.  
  30. public void addPartConverter(HttpMessageConverter<?> partConverter) {
  31. this.formHttpMessageConverter.addPartConverter(partConverter);
  32. }
  33.  
  34. @Override
  35. public boolean canRead(Class<?> clazz, MediaType mediaType) {
  36. return formHttpMessageConverter.canRead(clazz, mediaType);
  37. }
  38.  
  39. @Override
  40. public List<MediaType> getSupportedMediaTypes() {
  41. return formHttpMessageConverter.getSupportedMediaTypes();
  42. }
  43.  
  44. @Override
  45. public boolean canWrite(Class<?> clazz, MediaType mediaType) {
  46. if (!Map.class.isAssignableFrom(clazz)) {
  47. return false;
  48. }
  49. if (mediaType == null || MediaType.ALL.equals(mediaType)) {
  50. return true;
  51. }
  52. for (MediaType supportedMediaType : getSupportedMediaTypes()) {
  53. if (supportedMediaType.isCompatibleWith(mediaType)) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59.  
  60. @Override
  61. public Map<String, ?> read(Class<? extends Map<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
  62. return formHttpMessageConverter.read(null, inputMessage);
  63. }
  64.  
  65. public void write(Map<String, ?> map, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
  66. MultiValueMap<String, Object> multiMap = null;
  67. if (map != null) {
  68. if (map instanceof MultiValueMap) {
  69. multiMap = (MultiValueMap<String, Object>) map;
  70. } else {
  71. multiMap = new LinkedMultiValueMap<>();
  72. for (Map.Entry<String, ?> entry : map.entrySet()) {
  73. multiMap.add(entry.getKey(), entry.getValue());
  74. }
  75. }
  76. }
  77. formHttpMessageConverter.write(multiMap, contentType, outputMessage);
  78. }
  79.  
  80. public static class MultipartFormHttpMessageConverter extends FormHttpMessageConverter {
  81. @Override
  82. protected String getFilename(Object part) {
  83. String rt = super.getFilename(part);
  84. if (rt == null && part instanceof MultipartFile) {
  85. return ((MultipartFile) part).getOriginalFilename();
  86. }
  87. return null;
  88. }
  89. }
  90. }

  

RestTemplate OR Spring Cloud Feign 上传文件的更多相关文章

  1. spring cloud feign 上传文件报not a type supported by this encoder解决方案

    上传文件调用外部服务报错: not a type supported by this encoder 查看SpringFormEncoder类的源码: public class SpringFormE ...

  2. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  3. spring mvc CommonsMultipartResolver上传文件异常处理

    近期已经上线的项目出现了一个异常 严重: Servlet.service() for servlet JeeCmsAdmin threw exception org.apache.commons.fi ...

  4. springMVC+spring+hibernate注解上传文件到数据库,下载,多文件上传

    数据库 CREATE TABLE `annex` ( `id` bigint() NOT NULL AUTO_INCREMENT, `realName` varchar() DEFAULT NULL, ...

  5. spring boot(十七)上传文件

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

  6. 利用spring的CommonsMultipartResolver上传文件

    1.CommonsMultipartResolver是spring里面提供的一个上传方式,效率我不知道,但是加入spring容器管理还是很不错的. 2.先看依赖包pom.xml <project ...

  7. Spring使用mutipartFile上传文件报错【Failed to instantiate [org.springframework.web.multipart.MultipartFile]】

    报错场景: 使用SSM框架实现文件上传时报“Failed to instantiate [org.springframework.web.multipart.MultipartFile]”错,控制器源 ...

  8. Spring Boot (30) 上传文件

    文件上传 上传文件和下载文件是Java Web中常见的一种操作,文件上传主要是将文件通过IO流传输到服务器的某一个文件夹下. 导入依赖 在pom.xml中添加上spring-boot-starter- ...

  9. Spring Boot应用上传文件时报错

    问题描述 Spring Boot应用(使用默认的嵌入式Tomcat)在上传文件时,偶尔会出现上传失败的情况,后台报错日志信息如下:"The temporary upload location ...

随机推荐

  1. 【明哥报错簿】之【 "javax.servlet.http.HttpServlet" was not found on the Java Build Path || HttpServletRequest/HttpServletResponse cannot be resolved to a type】

    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path login ...

  2. TCP的拥塞控制 (三)

    1.   Multiple Packet Losses Fast Retransmit/Fast Recovery机制可以很好地处理单个packet丢失的问题,但当大量packet同时丢包时(一个RT ...

  3. Spring Boot系列教程五:使用properties配置文件实现多环境配置

    一.前言 实际项目开发过程中会用到多个环境,比如dev,test,product环境,不同的环境可能使用不同参数,为便于部署提高效率,本篇主要通过properties配置文件来实现多环境的配置. 二. ...

  4. 常州day1p3

    给定一个 n 行 m 列的方格,每个格子里有一个正整数 a,1 ≤ a ≤ k,k ≤ n∗m 假设你当前时刻站在 (i,j) 这个格子里,你想要移动到 (x,y),那必须满足以下三个条件 1:i & ...

  5. Extjs treePanel过滤查询功能【转】

    Extjs4.2中,对于treeStore中未实现filterBy函数进行实现,treestore并未继承与Ext.data.Store,对于treePanel的过滤查询功能,可有以下两种实现思路: ...

  6. Spring MVC @CookieValue注解

    @CookieValue的作用 用来获取Cookie中的值 @CookieValue参数 1.value:参数名称 2.required:是否必须 3.defaultValue:默认值 @Cookie ...

  7. array_uintersect、array_uintersect_assoc、array_uintersect_uassoc 的使用方法

    和 array_intersect 类似,只不过 array_uintersect* 系列函数的值比较使用自定义函数: 键的比较,array_uintersect.array_uintersect_a ...

  8. [ldap]slapcat/ldapsearch与ldap备份

    http://serverfault.com/questions/577356/ldap-backup-with-slapcat-vs-ldapsearch Used: openldap-server ...

  9. uboot&kernel&system

  10. js知识点乱炖

    修改属性 元素.style.样式=值     document.getElementById('box').style.width='200px'; 属性操作方式 1.. 的 元素.属性名如果属性是单 ...