2013-03-17 75 views
0

我试图上传图片到我的DB2 database.i已复制库需要 db2jcc.jarweb-inf/lib上传图像与servlet

<form action="Upload" method="post" enctype="multipart/form-data"> 
ID : <input type="text" name="id"/><br> 
FILE : <input type="file" name="photo"/><br> 
<input type="submit" value="upload"/> 
</form> 

我上传的servlet

try { 
     String id=request.getParameter("id"); 
     Part photo = request.getPart("photo"); 
     try 
     { 
      Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance(); 
      Connection con=DriverManager.getConnection("jdbc:db2:SAMPLEDB"); 
      System.out.println("Connection Successful"); 
      PreparedStatement ps=con.prepareStatement("INSERT INTO SAMPLETABLE (ID,PHOTO) VALUES (?,?)"); 
      ps.setString(1, id); 
      File fBlob = new File (request.getParameter("photo")); //exception thrown here 
      FileInputStream is = new FileInputStream (fBlob); 
      ps.setBinaryStream (2, is, (int) fBlob.length()); 
      ps.execute(); 

     } 
     catch(Exception e) 
     { 
      System.out.println("exception --> "+e); 
     } 
    } 
    finally 
    { 
     out.close(); 
    } 
} 

的我得到的例外是

java.lang.NullPointerException 

回答

1

Th e request.getParameter()及其相关方法不适用于多部分请求,并且在处理多部分表单数据时将始终返回null。

见在Java中对文件上传如下: https://www.coderanch.com/how-to/java/FileUpload

这里甚至有一个更好的解决方案:http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml

+0

没份重量DRE在roseindia.net ... BT它不是wrking ......可以ANY1 TL我d解决方案????????? – user1506349 2013-03-17 21:31:21