• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need a help to rectify my error  RSS feed

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page import="java.io.*,java.sql.*" %>
String contentType = request.getContentType();
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;

while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
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) + 2;
pos = file.indexOf("\n", pos) + 3;
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));
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database.</b>
<%Connection con=null;
Statement pst=null;
String line = null;
String value=null;
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
value = contents.toString();
int val = pst.executeUpdate("insert into file(file_data) values('"+value+"')");
catch(Exception e)

Am doing a csv file import & saving in database.two jsp pages are used,one is to browse & the other page is to save in the database.I got the output but the problem is that all the row values are geting added in the same column in database,where do i want to correct so that i get a proper output such that rows has to save in row wise & column has to get saved in column wise..

Can anyone get me out from this point..
Posts: 42972
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

Looking briefly at the code, I noticed the following issues. Firstly, you should never do this:

catch (Exception e)

How will you know if there are problems? At least print a message to a log file or console.

String file = new String(dataBytes);

This assumes that the file is encoded in the default encoding of the server JVM - which is quite likely not the case. Use the constructor that allows you to specify the encoding.

I would strongly suggest to use a CSV processing library (you can find several here); CSV has a number of features that aren't implemented by your code, and which might lead to problems later if they happen to occur within the file.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!