0

I'm in the early phases of creating my first blog with Java and, so far, I wrote two different JSPs: - NewArticle.jsp, where I can fill out all data to be displayed in the home page (article, title, data, links and images) - Home.jsp, displaying all information from NewArticle.

I am able to pass all text data from NewArticle to Home but unfortunately I'm unable to pass any image. I read dozens of posts about how this can be done, but I am still totally lost and I really need some help.

This is the upload form from NewArticle.jsp (I removed all input types=text) :

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>File Upload</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form method="POST" action="upload" enctype="multipart/form-data" >
            File:
            <input type="file" name="file" id="file" /> <br/>
            <input type="submit" value="Upload" name="upload" id="upload" />
        </form>
    </body>
</html>

This is the code I wrote in my servlet so far:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     Part filePart = request.getPart("file");
    String fileName = filePart.getSubmittedFileName();
    InputStream fileContent = filePart.getInputStream();

Unfortunately I am blocked here, because I don't know how to finalize the code in the servlet.

Any help, please?

Alex Reds
  • 197
  • 2
  • 4
  • 17
  • In case you want more helpful links what to do after obtaining the uploaded file, see bottom of http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824 – BalusC Nov 03 '15 at 19:48
  • Thank you for your reply, however what do you mean with "get your response output writer and send the data"? Moreover, can you please specify more in detail what I should use as parameter for the setContentType method (I guess "image/png" is just an example). – Alex Reds Nov 04 '15 at 09:25
  • I have no idea what you're talking about. Perhaps you was referring to a currently deleted poor quality answer? Ignore it and carefully read the duplicate and the above link. – BalusC Nov 04 '15 at 09:29
  • Yes, it was a reply to a different answer that I can not see anymore. However, as I wrote in my post, I read dozens of posts (including the one you suggested) but I still can not get how it works; I hoped somebody could help me to complete my code, since I'm totally lost. – Alex Reds Nov 04 '15 at 15:18
  • I suggest to take a break so you can look again with a fresh mind. Or, if you actually never programmed in Java, I suggest to pause JSP/Servlets and learn some basic Java first here https://docs.oracle.com/javase/tutorial Or if you actually never programmed before, I suggest to buy a book on programming. – BalusC Nov 04 '15 at 15:20

0 Answers0