Last active
June 28, 2017 00:35
-
-
Save localghost666/4407e74e6ba31ed8154abee9dba105a0 to your computer and use it in GitHub Desktop.
File upload in Servlet 3.0+ way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.MultipartConfig; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.Part; | |
/** | |
* File upload class(Servlet 3.0+ required) | |
* | |
*/ | |
@MultipartConfig(fileSizeThreshold=1024*1024, maxFileSize=1024*1024*10, maxRequestSize=1024*1024*20) | |
public class MediaController { | |
/** | |
* The directory to save submitted files(/{UPLOAD_DIR}) | |
*/ | |
private static final String UPLOAD_DIR = "uploads"; | |
/** | |
* Upload files | |
* @param req HttpServletRequest | |
* @param resp HttpServletResponse | |
* @return List of paths | |
* @throws ServletException | |
* @throws IOException | |
*/ | |
public List<String> fileUpload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
List<String> fileNames = new ArrayList<String>(); | |
// Make absolute path to save files | |
String uploadFilePath = req.getServletContext().getRealPath("") + UPLOAD_DIR; | |
// Make a directory if there isn't | |
File fileSaveDir = new File(uploadFilePath); | |
if (!fileSaveDir.exists()) { | |
fileSaveDir.mkdirs(); | |
} | |
for (Part part : req.getParts()) { | |
String fileName = getFileName(part); | |
String location = uploadFilePath + File.separator + fileName; | |
part.write(location); | |
fileNames.add(location); | |
part.delete(); | |
} | |
// Return list after writing files | |
return fileNames; | |
} | |
/** | |
* Delete files | |
* @param paths List of paths | |
* @return 삭제한 파일 갯수 | |
*/ | |
public int fileDelete(List<String> paths) { | |
int files = 0; | |
for (String path : paths) { | |
File file = new File(path); | |
file.delete(); | |
} | |
return files; | |
} | |
/** | |
* Get name of file from content-disposition header | |
* @param part | |
* @return MD5-encoded-file-name | |
*/ | |
private String getFileName(Part part) { | |
String[] fileName = null; | |
// Get content-disposition header | |
String[] cd = part.getHeader("content-disposition").split(";"); | |
for (String s : cd) { | |
if (s.trim().startsWith("filename")) { | |
String fn = s.substring(s.indexOf("=") + 1, s.length() - 1); | |
fileName = new String[2]; | |
fileName[0] = encodeFileName(fn.substring(0, fn.lastIndexOf("."))); | |
fileName[1] = fn.substring(fn.lastIndexOf(".") + 1); | |
return fileName[0] + "." + fileName[1]; | |
} | |
} | |
return null; | |
} | |
/** | |
* Encode with MD5 | |
* @param fileName | |
* @return | |
*/ | |
private String encodeFileName(String fileName) { | |
StringBuffer newFileName = new StringBuffer(); | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
md.update(fileName.getBytes("UTF-8")); | |
byte[] ba = md.digest(); | |
for (byte b : ba) { | |
newFileName.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); | |
} | |
return newFileName.toString().toUpperCase(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
newFileName = null; | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment