Created
April 9, 2015 19:58
-
-
Save alexandreaquiles/999b18cacee33962433e to your computer and use it in GitHub Desktop.
Servlet que faz upload de arquivos no diretório configurado no init-param "diretorio", utilizando apenas recursos da Servlet 3.0. Interessante comparar com: https://gist.github.com/alexandreaquiles/8564914
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
<html> | |
<body> | |
<form action="upload" method="post" enctype="multipart/form-data"> | |
<input type="text" name="texto"> | |
<input type="file" name="arquivo"> | |
<input type="submit" value="Upload"> | |
</form> | |
</body> | |
</html> |
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 upload; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.nio.file.Files; | |
import java.nio.file.StandardCopyOption; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import javax.servlet.ServletConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.MultipartConfig; | |
import javax.servlet.annotation.WebInitParam; | |
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; | |
@WebServlet(urlPatterns = "/upload", initParams = { @WebInitParam(name = "diretorio", value = "/home/alexandre/uploads") }) | |
@MultipartConfig | |
public class UploadArquivoServlet extends HttpServlet { | |
private static final String FILENAME_REGEX = ".*filename=\"(.*)\".*"; | |
private File diretorio; | |
@Override | |
public void init(ServletConfig config) throws ServletException { | |
super.init(config); | |
String path = config.getInitParameter("diretorio"); | |
diretorio = new File(path); | |
diretorio.mkdirs(); | |
} | |
@Override | |
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
PrintWriter out = response.getWriter(); | |
try { | |
for (Part part : request.getParts()) { | |
String fileName = obtainFileName(part); | |
if (fileName != null) { | |
if (part.getSize() <= 0) { | |
out.println("<h1>Voce não enviou um arquivo!</h1>"); | |
return; | |
} | |
processUploadedFile(part, fileName); | |
} else { | |
// para inputs que nao sao 'file', fileName é null | |
String nomeDoCampo = part.getName(); | |
String valorDoCampo = request.getParameter(nomeDoCampo); | |
System.out.println(nomeDoCampo + ": " + valorDoCampo); | |
} | |
} | |
out.println("<h1>Arquivo gravado!</h1>"); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
out.println("<h1>Erro ao escrever no arquivo!</h1>"); | |
return; | |
} | |
} | |
private void processUploadedFile(Part part, String fileName) throws Exception { | |
InputStream inputStream = part.getInputStream(); | |
File uploadedFile = new File(diretorio, fileName); | |
Files.copy(inputStream, uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | |
} | |
private String obtainFileName(Part part) { | |
String contentDisposition = part.getHeader("content-disposition"); | |
Pattern regex = Pattern.compile(FILENAME_REGEX); | |
Matcher matcher = regex.matcher(contentDisposition); | |
if (matcher.find()) { | |
return matcher.group(1); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment