From 34f9a7dad5535870486edb304ee8043381f7cbdd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 8 Dec 2016 16:14:34 +0300 Subject: juick-api: fix upload --- .../java/com/juick/api/configuration/ApiMvcConfiguration.java | 8 ++++++++ juick-api/src/main/java/com/juick/api/controllers/Post.java | 8 ++++---- juick-api/src/main/java/com/juick/api/util/HttpUtils.java | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) (limited to 'juick-api/src/main/java/com') diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java index 549de8bc..5a953c1f 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiMvcConfiguration.java @@ -2,11 +2,13 @@ package com.juick.api.configuration; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @@ -38,4 +40,10 @@ public class ApiMvcConfiguration extends WebMvcConfigurationSupport { converters.add(converter); super.configureMessageConverters(converters); } + @Bean + public CommonsMultipartResolver multipartResolver() { + CommonsMultipartResolver resolver = new CommonsMultipartResolver(); + resolver.setMaxUploadSize(10000000); + return resolver; + } } diff --git a/juick-api/src/main/java/com/juick/api/controllers/Post.java b/juick-api/src/main/java/com/juick/api/controllers/Post.java index 9749d700..40c6207e 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Post.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Post.java @@ -111,7 +111,7 @@ public class Post { } } - String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; + String attachmentType = StringUtils.isNotEmpty(attachmentFName) ? attachmentFName.substring(attachmentFName.length() - 3) : null; int mid = messagesService.createMessage(vuid, body, attachmentType, tagsList); subscriptionService.subscribeMessage(mid, vuid); com.juick.Message jmsg = messagesService.getMessage(mid); @@ -124,7 +124,7 @@ public class Post { xmsg.addExtension(jmsg); xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); - if (attachmentFName != null) { + if (StringUtils.isNotEmpty(attachmentFName)) { String fname = mid + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; @@ -227,7 +227,7 @@ public class Post { } } - String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; + String attachmentType = StringUtils.isNotEmpty(attachmentFName) ? attachmentFName.substring(attachmentFName.length() - 3) : null; int ridnew = messagesService.createReply(mid, rid, vuid, body, attachmentType); subscriptionService.subscribeMessage(mid, vuid); @@ -247,7 +247,7 @@ public class Post { xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); - if (attachmentFName != null) { + if (StringUtils.isNotEmpty(attachmentFName)) { String fname = mid + "-" + ridnew + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; diff --git a/juick-api/src/main/java/com/juick/api/util/HttpUtils.java b/juick-api/src/main/java/com/juick/api/util/HttpUtils.java index 2f8ca7c5..93fb7874 100644 --- a/juick-api/src/main/java/com/juick/api/util/HttpUtils.java +++ b/juick-api/src/main/java/com/juick/api/util/HttpUtils.java @@ -38,7 +38,7 @@ public class HttpUtils { private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); public static String receiveMultiPartFile(MultipartFile attach, String tmpDir) { - if (!attach.isEmpty()) { + if (attach !=null && !attach.isEmpty()) { String partname = attach.getOriginalFilename(); if (partname != null && partname.length() > 0) { String attachmentType = partname.substring(partname.length() - 3).toLowerCase(); -- cgit v1.2.3