Java Reference
In-Depth Information
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
final String filepath = request.getParameter("destination");
final Part filePart = request.getPart("myfile");
final String fileName = getFileName(filePart);
final PrintWriter writer = response.getWriter();
try(
OutputStream out = new FileOutputStream(new File(filepath + File.separator + fileName));
InputStream filecontent = filePart.getInputStream()){
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
writer.println("New file " + fileName + " created at " + filepath);
LOGGER.log(Level.INFO, "File{0}being uploaded to {1}",
new Object[]{fileName, filepath});
out.flush();
} catch (FileNotFoundException fne) {
writer.println("You either did not specify a file to upload or are " +
"trying to upload a file to a protected or nonexistent location.");
writer.println("<br/> ERROR: " + fne.getMessage());
Search WWH ::




Custom Search