on 02-23-2007 6:00 AM
Hello;
I have following code. and i can easily upload in apache tomcat server (it doesnt care about file size) but after i deploy portal i cannot upload if file size above 30kb. And i get this error:
<b>Cannot read from InputStream. The buffer is null or the offset and length are incorrect</b>
Is there any idea?
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
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;
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;
saveFile="c:\"+saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " + saveFile);
}
%>
Hi Eray,
I completely agree with Vivek. <a href="http://jakarta.apache.org/commons/fileupload/">Commons FileUpload</a> is very good. In case you don't want to use it, you could have a look at the source code.
Note that Integer.MAX_VALUE == 2^31-1 == 2147483647. Since request.getContentLength() returns the number of bytes, the maximum value is almost 2 GB.
Anyway, there's at least one bug in your code. It should be
byteRead = in.read(dataBytes, totalBytesRead, formDataLength - totalBytesRead);
Besides this, I'm wondering why you use a DataInputStream instead of just an InputStream (it's the responsability of the servlet container to provide an efficient implementation like a BufferedInputStream). I'm not sure what you're trying to do next, but it looks like you save the file locally. The implementation looks a bit buggy though...
Kind regards,
Sigiswald
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks, also i have found second program
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
There is a very good open-source framework from Apache called FileUpload its good and easy to use for this kind of purpose.
Hope this helps
regards
Vivek Nidhi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
found problem in.read statement. because if my read parameter above integer.maxvalue(32768) it gives null exception. how can i handle it. is there any idea.
also i have tried read by 1 byte but result same! i cannot read if file size over 32kb
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.