2016-02-13 22 views
1

我想獲取客戶端上傳的圖像文件,並讓他們將其存儲在遠程服務器中。代碼全部設置良好,並且可以在硬編碼文件路徑時將圖像保存爲Blob。保存Html圖像輸入爲一個java文件

但是,如何將HTML文件(圖像)作爲文件輸入?我保存字符串值如下工作,但什麼是我想要保存圖像作爲文件在Java中的等價物?

我嘗試了以下的地方我已評論但它返回錯誤,如下所示。

類型不匹配:不能從字符串轉換成文件

<!-- 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) 
+0

可能的重複[如何將字符串保存到使用Java的文本文件?](http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-toa-a- text-file-using-java) –

+0

@Elogent這是如何重複的?我遇到了保存圖像的問題。 – kar

+0

您在您的問題中表示您正在嘗試保存「HTML文件」。 HTML是文本。此外,你說你已經*有一個字符串,所以剩下的唯一步驟就是將該字符串保存到一個文本文件中。 –

回答

1

你有沒有設置內容類型multipart/form-data?如果您的表單包含元素,則這是必需的。

+0

不,我沒有。謝謝,謝謝。不要認爲它解決了問題,但這會導致我相信的問題。謝謝。 – kar