aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/util
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-28 13:09:34 +0300
committerGravatar Vitaly Takmazov2016-11-28 13:09:34 +0300
commit1679b35661297fd9a6693b03cadcdbc1ab5a4203 (patch)
treeec2457286fcf93f1c227da369c0c39c98aa853df /juick-api/src/main/java/com/juick/api/util
parentbc23d2d2125d2086847397e85335f29a70668f6b (diff)
juick-api: all controllers are using spring-security and @RequestParam
Diffstat (limited to 'juick-api/src/main/java/com/juick/api/util')
-rw-r--r--juick-api/src/main/java/com/juick/api/util/HttpUtils.java32
1 files changed, 20 insertions, 12 deletions
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 f20e10a8..2fe4322d 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
@@ -17,12 +17,19 @@
*/
package com.juick.api.util;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.multipart.MultipartFile;
+
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.UUID;
/**
@@ -30,6 +37,7 @@ import java.util.UUID;
* @author Ugnich Anton
*/
public class HttpUtils {
+ private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
public static String getPartFilename(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
@@ -39,27 +47,27 @@ public class HttpUtils {
}
return null;
}
- public static String receiveMultiPartFile(HttpServletRequest request, String name) throws Exception {
- String attachmentFName = null;
-
- Part filePart = request.getPart("attach");
- if (filePart != null) {
- String partname = HttpUtils.getPartFilename(filePart);
+ public static String receiveMultiPartFile(MultipartFile attach, String tmpDir) {
+ if (!attach.isEmpty()) {
+ String partname = attach.getOriginalFilename();
if (partname != null && partname.length() > 0) {
String attachmentType = partname.substring(partname.length() - 3).toLowerCase();
if (attachmentType.equals("jpg") || attachmentType.equals("peg") || attachmentType.equals("png")) {
if (attachmentType.equals("peg")) {
attachmentType = "jpg";
}
- attachmentFName = UUID.randomUUID().toString() + "." + attachmentType;
- filePart.write("/var/www/juick.com/i/tmp/" + attachmentFName);
- } else {
- throw new Exception("Wrong file type");
+ String attachmentFName = UUID.randomUUID().toString() + "." + attachmentType;
+ try {
+ Files.write(Paths.get(tmpDir, attachmentFName),
+ attach.getBytes());
+ return attachmentFName;
+ } catch (IOException e) {
+ logger.warn("file receive error", e);
+ }
}
}
}
-
- return attachmentFName;
+ return "";
}
public static String downloadImage(URL url) throws Exception {
String attachmentFName = null;