From cfd873f5c5e52eb89b7985555afdb1a180e71620 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 5 Sep 2016 01:35:19 +0300 Subject: avatar uploading --- juick-www/src/main/java/com/juick/www/Utils.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'juick-www/src/main/java/com/juick/www/Utils.java') diff --git a/juick-www/src/main/java/com/juick/www/Utils.java b/juick-www/src/main/java/com/juick/www/Utils.java index ee2fa212..4be659cb 100644 --- a/juick-www/src/main/java/com/juick/www/Utils.java +++ b/juick-www/src/main/java/com/juick/www/Utils.java @@ -21,6 +21,7 @@ import com.juick.User; import com.mitchellbosecke.pebble.PebbleEngine; import org.springframework.jdbc.core.JdbcTemplate; +import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -28,6 +29,7 @@ import javax.servlet.http.Part; import java.io.*; import java.net.URL; import java.net.URLConnection; +import java.nio.file.Paths; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,6 +44,7 @@ public class Utils { private static final Logger logger = Logger.getLogger(Utils.class.getName()); private static final PebbleEngine engine = new PebbleEngine.Builder().build(); + private static String tmpDir = "/var/www/juick.com/i/tmp/"; public static String getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); @@ -55,10 +58,10 @@ public class Utils { return null; } - public static String receiveMultiPartFile(HttpServletRequest request, String name) throws Exception { + public static String receiveMultiPartFile(HttpServletRequest request, String name) throws IOException, ServletException { String attachmentFName = null; - Part filePart = request.getPart("attach"); + Part filePart = request.getPart(name); if (filePart != null) { String partname = Utils.getPartFilename(filePart); if (partname != null && partname.length() > 0) { @@ -68,9 +71,9 @@ public class Utils { attachmentType = "jpg"; } attachmentFName = UUID.randomUUID().toString() + "." + attachmentType; - filePart.write("/var/www/juick.com/i/tmp/" + attachmentFName); + filePart.write(Paths.get(getTmpDir(), attachmentFName).toString()); } else { - throw new Exception("Wrong file type"); + throw new IOException("Wrong file type"); } } } @@ -299,4 +302,12 @@ public class Utils { public static PebbleEngine getEngine() { return engine; } + + public static String getTmpDir() { + return tmpDir; + } + + public static void setTmpDir(String tmpDir) { + Utils.tmpDir = tmpDir; + } } -- cgit v1.2.3