1

I am trying to get an image file uploaded by the client and let them store it in a remote server. The code is all set up fine and able to save the image as a Blob when I hard code the file path.

But how do I store the HTML file(image) input as a File? I save String values as follows which works but whats the equivalent when I want to save an image as a File in Java?

I tried the following where I have commented but it returns an error as follows.

Type mismatch: cannot convert from String to File

<!-- HTML Form where the File is being uploaded --> 
<form method="post" action="process.jsp">
    <div class="form-group">
        <input type="text" class="form-control" id="contact" name="contact">
    </div>  
    <div class="form-group">
        <input type="file" class="form-control" id="file" name="file">
    </div>
    <button type="submit" class="btn">Submit</button>
</form>


//process.jsp
<body>
<%
    //String works. 
    String contact = request.getParameter("contact");

    //Harcoding file path works.
    File img = new File("C:\\Users\\username\\Desktop\\a.jpg");

    //**ERROR** - Trying this but returns error mentioned above. 
    //File img= request.getParameter("file");

    PreparedStatement stmt;
    FileInputStream fis;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://1.1.1.1:3306/something",
                "username", "password");

        stmt = conn.prepareStatement(
                "INSERT INTO table(contact, image) values (?, ?)");
        stmt.setString(1, contact);

        fis = new FileInputStream(img);
        stmt.setBinaryStream(2, fis, (int)(img.length()));
        stmt.executeUpdate();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
%>
</body>

//Full Error message: 

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 24 in the jsp file: /process.jsp
Type mismatch: cannot convert from String to File
21:     String contact= request.getParameter("contact");
22:     //File img = new File("C:\\Users\\username\\Desktop\\a.jpg");
23: 
24:     File img = request.getParameter("file");
25: 
26:     PreparedStatement stmt;
27:     FileInputStream fis;


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
kar
  • 4,791
  • 12
  • 49
  • 74
  • Possible duplicate of [How do I save a String to a text file using Java?](http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java) – Sam Estep Feb 13 '16 at 03:07
  • @Elogent How is this a duplicate of that? I am having issue with saving an image. – kar Feb 13 '16 at 03:08
  • You said in your question that you are trying to save "the HTML file". HTML is text. Furthermore, you said that you already *have* a string, so the only remaining step is to save that string to a text file. – Sam Estep Feb 13 '16 at 03:09
  • @Elogent I am giving that String as an example which works. My issue is with saving the image. Maybe read the question again. – kar Feb 13 '16 at 03:13
  • It would help if you were to include more information about the error you are getting. – Sam Estep Feb 13 '16 at 03:14
  • @Elogent Added the full trace if it helps. – kar Feb 13 '16 at 03:19
  • Google for "jsp upload file". There are hundreds of tutorials. It seems you didn't try any of them. – vanje Feb 13 '16 at 10:51

1 Answers1

1

Have you set the content type to multipart/form-data? This is required if your form includes elements.

Community
  • 1
  • 1
Marko Kajzer
  • 106
  • 1
  • 5
  • No I didn't. Good catch thanks. Don't think it resolves issue but this would have caused problems I believe. Thanks. – kar Feb 13 '16 at 03:23