Posted By:
Madhavi_Katragadda
Posted On:
Wednesday, March 20, 2002 04:32 AM
I am trying to upload a file using servletsThe problem I am facing is that my file is getting corrupted. I am able to upload the file, but the file is corrupted. i am trying to upload word documents or excel sheets. I am using mime type text/plain and using DataInputStream to read the files into bytes and wriing the bytes using FileOutputStream. I am using the code in Dustin R Callaway Text Book. My servlet code runs something like this.. --- --- in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); if(formDataLength > MAX_SIZE) { out.println("Sorry, File is too la
More>>
I am trying to upload a file using servletsThe problem I am facing is that my file is getting corrupted.
I am able to upload the file, but the file is corrupted. i am trying to upload word documents or excel sheets.
I am using mime type text/plain and using DataInputStream to read the files into bytes and wriing the bytes using FileOutputStream.
I am using the code in Dustin R Callaway Text Book.
My servlet code runs something like this..
---
---
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength > MAX_SIZE)
{
out.println("Sorry, File is too large to upload");
out.flush();
return;
}
//Allocate a byte array to store content data
byte dataBytes[] = new byte[formDataLength];
//Read file into byte array.
int bytesRead = 0;
int totalBytesRead = 0;
while( totalBytesRead
< formDataLength )
{
bytesRead = in.read(dataBytes, totalBytesRead, formDataLength);
//Keep track of total bytes read from input stream
totalBytesRead += bytesRead;
}
//Create String from byte array for easy manipulation
String file = new String(dataBytes);
---
---
fileOut = new FileOutputStream(fileName);
//Write the string to the file as a byte array
fileOut.write(file.getBytes(),0,file.length());
---
---
File is getting created in required directory but is corrupted.
Please help me with this problem.
Thanks in advance
Madhavi K
<<Less