将HTML图像输入另存为Java文件

kar

我正在尝试获取客户端上传的图像文件,并让他们将其存储在远程服务器中。代码设置都很好,当我对文件路径进行硬编码时,能够将图像另存为Blob。

但是,如何将输入的HTML文件(图像)存储为文件?我按如下方式保存String值,但可以工作,但是当我想将图像保存为Java中的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)
马克·凯泽(Marko Kaiser)

您是否已将内容类型设置为multipart / form-data如果您的表单包含元素,则这是必需的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章