Created
August 30, 2010 04:49
-
-
Save SeanJA/557021 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
/** | |
* Here an example using servlet 3.0 to upload file with file-uploader | |
*/ | |
package com.silyan.simplex.view.site.content; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.PrintWriter; | |
import javax.servlet.ServletConfig; | |
import javax.servlet.ServletException; | |
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; | |
/** | |
* Example of Servlet 3.0 used for valums-file-uploader. | |
* | |
* Patch problem generated by use "multipart/form-data" request in same browsers and "application/x-www-form-urlencoded" in others. | |
* By this problem, could not use @MultipartConfig features. | |
* | |
* Other solution is use two servlets, one for "multipart/form-data" request and other one for "application/x-www-form-urlencoded" request, and | |
* change url target in client (using javascript to detect browser). | |
* | |
* @author Angel Cervera Claudio (angelcervera@silyan.com) | |
* | |
*/ | |
@WebServlet(name="UploadResourceServlet", urlPatterns="/upload", | |
initParams={ | |
@WebInitParam(description="Max size in bytes", name="maxLength", value="2097152") /*2Mb = 1024×1024×2*/, | |
@WebInitParam(description="Path where will be stored files", name="targetPath", value="/tmp/") | |
} | |
) | |
public class UploadResourceServlet extends HttpServlet { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = -4887583429734715110L; | |
/** | |
* limit length. | |
*/ | |
private Long maxLength; | |
/** | |
* Path where will be stored files | |
*/ | |
private String targetPath; | |
/* (non-Javadoc) | |
* @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) | |
*/ | |
@Override | |
public void init(ServletConfig servletConfig) throws ServletException { | |
super.init(servletConfig); | |
maxLength = new Long( getInitParameter("maxLength") ); | |
targetPath = getInitParameter("targetPath"); | |
if(targetPath.lastIndexOf(File.separatorChar) != 0) { | |
targetPath = targetPath + File.separatorChar; | |
} | |
} | |
/* (non-Javadoc) | |
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
String length = request.getHeader("content-length"); | |
if( length == null || Long.valueOf(length) > maxLength) { | |
PrintWriter pw = response.getWriter(); | |
pw.write("{\"error\":\"Max length (" + maxLength+ "bytes) exceed or it's null\"}"); | |
return; | |
} | |
if( isMultipartContent(request) ) { | |
Part part = request.getPart("qqfile"); | |
String fileName = getFileNameFromPart(part); | |
writeUploadedResource(fileName, part.getInputStream()); | |
} else { | |
writeUploadedResource(request.getParameter("qqfile"), request.getInputStream()); | |
} | |
} | |
/** | |
* Write in target directory. | |
* | |
* @param fileName | |
* @param is | |
* @throws FileNotFoundException | |
* @throws IOException | |
*/ | |
private void writeUploadedResource(String fileName, InputStream is) throws FileNotFoundException, IOException { | |
OutputStream os = new FileOutputStream(targetPath + fileName ); | |
byte[] buffer = new byte[4096]; | |
long count = 0; | |
int n = 0; | |
while (-1 != (n = is.read(buffer))) { | |
os.write(buffer, 0, n); | |
count += n; | |
} | |
os.flush(); | |
os.close(); | |
is.close(); | |
} | |
/** | |
* Utility method that determines whether the request contains multipart | |
* content. | |
* | |
* Extract from commons-fileupload source. | |
* | |
* @param request The servlet request to be evaluated. Must be non-null. | |
* | |
* @return <code>true</code> if the request is multipart; | |
* <code>false</code> otherwise. | |
*/ | |
public static final boolean isMultipartContent( HttpServletRequest request) { | |
if (!"post".equals(request.getMethod().toLowerCase())) { | |
return false; | |
} | |
String contentType = request.getContentType(); | |
if (contentType == null) { | |
return false; | |
} | |
if (contentType.toLowerCase().startsWith("multipart/")) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Return filename from part. | |
* | |
* @param part | |
* @return | |
*/ | |
public static final String getFileNameFromPart(Part part) { | |
for (String cd : part.getHeader("content-disposition").split(";")) { | |
if (cd.trim().startsWith("filename")) { | |
return cd.substring(cd.indexOf('=') + 1).trim().replace("\"", ""); | |
} | |
} | |
return null; | |
} | |
} |
response.getWriter().append("{success:true}"); is wrong
response.getWriter().append("{"success":true}"); is Ok
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
patch: In the end of doPost method, is needed return "success", so it's necesary add tow lines:
response.setContentType("text/html");
response.getWriter().append("{success:true}");