0

We have to upload file via http PUT due to some limitation on the client-side(No POST method).

There has a thread talking about this on stackoverflow as below

File Upload via HTTP PUT Request

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.

I have created a servlet for doPut as below 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)
    {}
}
}

And use simple http PUT by HttpClient as below

    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()));

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.

Any help is appreciated.

Community
  • 1
  • 1
Bruce
  • 647
  • 2
  • 12
  • 30
  • 2
    You may use Apache Commons Fileupload : https://commons.apache.org/proper/commons-fileupload/ – Arnaud Jan 05 '16 at 07:57
  • 1
    Commons fileupload is nice, but Servlet 3.0 exist since 2009 already. In the duplicate answer, just substitute POST with PUT. Noted should be that yours is a really bad example of PUT. The meaning of PUT is that exactly the same resource is obtainable by GET on exactly the same URI. This is not required for POST. – BalusC Jan 05 '16 at 08:06

0 Answers0