Last active
August 7, 2017 18:21
-
-
Save jyeary/fe86cae34e566fc85e6f805d42e0b31a to your computer and use it in GitHub Desktop.
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 com.bluelotussoftware.example.servlet; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.MultipartConfig; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.Part; | |
/** | |
* | |
* @author John Yeary | |
* @version 1.0 | |
*/ | |
@WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"}) | |
@MultipartConfig(location = "/tmp") | |
public class FileUploadServlet extends HttpServlet { | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
for (Part part : request.getParts()) { | |
String filename = ""; | |
for (String s : part.getHeader("content-disposition").split(";")) { | |
if (s.trim().startsWith("filename")) { | |
filename = s.split("=")[1].replaceAll("\"", ""); | |
} | |
} | |
part.write(filename); | |
} | |
} | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>File Upload Example</title> | |
</head> | |
<body> | |
<form action="FileUploadServlet" enctype="multipart/form-data" method="POST"> | |
<input type="file" name="file"> | |
<input type="Submit" value="Upload File"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment