Hi there guys!
I've been stuck with this problem for quite a while now.
I wanted to create a jsp which would upload files to sql database.
I've used this code for my upload_page.jsp:
<%--
Document : upload_page
Created on : Mar 6, 2011, 11:01:40 PM
Author : Administrator
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat, com.dole.cdms.util.ConnectionFactory;" %>
<html>
<%
int val = 0;
String contentType = request.getContentType();
System.out.println("content type is:" + contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
System.out.println("length of file is:" + formDataLength);
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
//out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " + saveFile);
// String saveFile = file.substring(file.indexOf("filename=\"") + 10);
// System.out.println("saveFile=" + saveFile);
// saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1 , saveFile.indexOf("\""));
// System.out.println("saveFile" + saveFile);
// saveFile = file.substring(file.indexOf("filename=\"") + 10);
// saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
// saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
// int lastIndex = contentType.lastIndexOf("=");
// String boundary = contentType.substring(lastIndex + 1, contentType.length());
// int pos;
//pos = file.indexOf("filename=\"");
//pos = file.indexOf("\n", pos) + 1;
// pos = file.indexOf("\n", pos) + 1;
// pos = file.indexOf("\n", pos) + 1;
// int boundaryLocation = file.indexOf(boundary, pos) - 4;
// int startPos = ((file.substring(0, pos)).getBytes()).length;
// int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
// FileOutputStream fileOut = new FileOutputStream(saveFile);
// fileOut.write(dataBytes, startPos, (endPos - startPos));
%>
<%
Connection con = null;
Statement statement = null;
//ResultSet rs=null;
String line = null;
String value = null;
try {
con = ConnectionFactory.getConnection();
statement = con.createStatement();
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while ((line = input.readLine()) != null) {
contents.append(line);
}
value = contents.toString();
System.out.println("Value:" + value);
// java.util.Date now = new java.util.Date();
// String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
// SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
// String strDateNew = sdf.format(now);
int i = statement.executeUpdate("INSERT INTO tblCNRDoc (cnrNo,cnrDocName,cnrDoc) VALUES('AGB-11-00003','"+ saveFile+"','" + value + "')");
// String queryString = "INSERT INTO file_tbl set file_data='" + value + "',file_date='" + strDateNew + "'";
//out.println(queryString);
//pstatement = con.prepareStatement(queryString);
// val = pstatement.executeUpdate();
if (val > 0) {
%>
<br><br>
<b>File <% out.println(saveFile);%> has been uploaded and inserted into Database.</b>
<%
}
} catch (Exception e) {
System.out.println("error:" + e);
}
}
%>
</html>
And this is by the way my initial jsp page:
<%@ page language="java" %>
<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION=
"upload_page.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><INPUT TYPE="submit" VALUE="Send File" ></td>
</tr>
</table>
</center>
</FORM>
</BODY>
</HTML>
Insert is successful for 1KB files only.. :(
I'm wondering why it won't go for other files.. :(
I'm getting this error message on my console:
error:java.sql.SQLException: Unexpected parameter marker at position 364.
Would really appreciate your help guys.. :)
Thanks much!