cancel
Showing results for 
Search instead for 
Did you mean: 

file Upload in Portal

former_member187444
Participant
0 Kudos

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);
    }
%>

Accepted Solutions (1)

Accepted Solutions (1)

Sigiswald
Contributor
0 Kudos

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

Answers (3)

Answers (3)

former_member187444
Participant
0 Kudos

thanks, also i have found second program

Former Member
0 Kudos

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

former_member187444
Participant
0 Kudos

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