aspose file tools*
The moose likes Servlets and the fly likes File upload using Servlets. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "File upload using Servlets." Watch "File upload using Servlets." New topic
Author

File upload using Servlets.

Rajkumar Gandhi
Greenhorn

Joined: Mar 15, 2013
Posts: 2
Hi,

I'm developing a web application to upload files(any type of files such as PDF, JPEG,etc.,).
For this I'm using
1. servlet version 2.5
2. commons-file upload

At back end to store files, I'm using DB2.

Files are to be stored in BLOB data type.

When I'm trying to upload files using simple java application, I'm able to insert the blob files into the database.
But, when I'm using web application, I'm getting exception as follows,

com.ibm.db2.jcc.am.SqlDataException: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=4.14.113
at com.ibm.db2.jcc.am.ed.a(ed.java:669)
at com.ibm.db2.jcc.am.ed.a(ed.java:60)
at com.ibm.db2.jcc.am.ed.a(ed.java:127)
at com.ibm.db2.jcc.am.oo.b(oo.java:2366)
at com.ibm.db2.jcc.am.oo.c(oo.java:2349)
at com.ibm.db2.jcc.t4.ab.l(ab.java:370)
at com.ibm.db2.jcc.t4.ab.a(ab.java:62)
at com.ibm.db2.jcc.t4.p.a(p.java:50)
at com.ibm.db2.jcc.t4.rb.b(rb.java:220)
at com.ibm.db2.jcc.am.po.qc(po.java:3497)
at com.ibm.db2.jcc.am.po.b(po.java:4458)
at com.ibm.db2.jcc.am.po.ic(po.java:799)
at com.ibm.db2.jcc.am.po.executeUpdate(po.java:777)
at com.hcl.vicroads.file_attachment_manager.controller.FileUploadServlet.doGet(FileUploadServlet.java:114)
at com.hcl.vicroads.file_attachment_manager.controller.FileUploadServlet.doPost(FileUploadServlet.java:166)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

Code which I'm using to insert blob files.(Same code in both web application and simple java application):
====================================================================
File file = new File("D:\\Write\\midblock dev.jpg");
long len1 = file.length();
FileInputStream fis = new FileInputStream(file);
byte[] file_content = new byte[(int)len1];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("Length: " + len1);
for(int readNum = 0; (readNum = fis.read(file_content)) != -1;) {
baos.write(file_content, 0, readNum);
System.out.println("Writing into byte array" + readNum);
}

file_content =baos.toByteArray();
System.out.println(file_content.length);
Blob blob_file = new SerialBlob(file_content);
fis.close();
Class.forName("com.ibm.db2.jcc.DB2Driver");

String dburl = "jdbc:db2://HCLSEZ-COE-VM50:50000/TIS_DWHD";
String username = "root";
String pwd = "pwd";
Connection con = DriverManager.getConnection(dburl, username, pwd);
PreparedStatement pre_stmt = con.prepareStatement("insert into \"db2DWH\".\"TIS_SRVY_DATA_RQST_ATTACHMENT\"(SRVY_DATA_RQST_ID, ATTACHMENT_NM, ATTACHMENT_TYP, ATTACHMENT_DESC, ATTACHMENT_FILE, INS_TIMESATMP, INS_USR, UPD_TMS, UPD_USR) values(?, ?, ?, ?, ?, ?, ?, ?, ?)");
pre_stmt.setInt(1, 1);
pre_stmt.setString(2, file_name);
pre_stmt.setString(3, file_item.getContentType());
pre_stmt.setString(4, null);
pre_stmt.setBlob(5, new SerialBlob(blob_file));
pre_stmt.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
pre_stmt.setString(7, "Java_Dev_Team");
pre_stmt.setTimestamp(8, null);
pre_stmt.setString(9, null);
pre_stmt.executeUpdate();


Please help me to get rid of this error. Struggling with this code for the last one week..

Thanks,
Rajkumar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: File upload using Servlets.