Java Reference
In-Depth Information
LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}",
new Object[]{fne.getMessage()});
} finally {
if (writer != null) {
writer.close();
}
}
}
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(
content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}
}
In the Servlet 3.1 release, the specification has been updated to include support for containers that do not
provide multipart/form-data processing. If the container does not provide the processing, then the data will be
available via HttpServletRequest.getInputStream . However, if the container does provide support, then the data
will be made available through the following methods in HttpServletRequest , as shown in the previous example:
public Collection<Part> getParts();
public Part getPart(String name);
The following example code demonstrates how a servlet without multipart/form-data processing may be coded:
...
protected void processRequestNoMultipart(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
final String filepath = request.getParameter("destination");
final String path = request.getServletPath();
final String fileName = getFileName(path, request);
final PrintWriter writer = response.getWriter();
try(
InputStream filecontent = request.getInputStream();
OutputStream out = new FileOutputStream(new File(filepath + File.separator +
request.getLocalName()))){
int read = 0;
 
Search WWH ::




Custom Search