A Java servlet for uploading file via http PUT <p>We have to upload file via http PUT due to some limitation on the client-side(No POST method).</p> <p>There has a thread talking about this on stackoverflow as below</p> <p><a href="http://stackoverflow.com/questions/18728100/file-upload-via-http-put-request">File Upload via HTTP PUT Request</a></p> <p>But we ain't using Spring MVC, so that I don't know how to get this way works by using the AbstractHttpMessageConverter to convert the request body into file as the answer in above link.</p> <p>I have created a servlet for doPut as below code</p> <pre><code>public class FileUploader extends HttpServlet { private static Logger logger = LogManager.getLogger(FileUploader.class); /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter output = resp.getWriter(); output.println("GET is not support"); output.close(); } @Override public void doPut(HttpServletRequest req, HttpServletResponse res) { try { PrintWriter outHTML = res.getWriter(); outHTML.println("You have put a file!"); int i; InputStream input; input = req.getInputStream(); BufferedInputStream in = new BufferedInputStream(input); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); File outputFile = new File("C:\\tmp.txt"); FileWriter out = new FileWriter(outputFile); while ((i = reader.read()) != -1) { System.out.print(i); out.write(i); } out.close(); in.close(); outHTML.println("You have put a file! But nothing else done here."); logger.debug("File write ok"); } catch (Exception e) {} } } </code></pre> <p>And use simple http PUT by HttpClient as below</p> <pre><code> HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient client = builder.build(); HttpPut put = new HttpPut(""); put.setURI(URI.create("http://localhost:1010/fileUploader")); System.out.println(put.getRequestLine()); put.setEntity(new FileEntity(new File("C:\\Users\\Administrator\\Desktop\\123.7z"))); CloseableHttpResponse response = client.execute(put); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); </code></pre> <p>Yes, the file has been PUT and write to C:\ as tmp.txt, but still didn't know hot to retrieve the file and get the name of it that client has uploaded.</p> <p>Any help is appreciated.</p>