file Uploading

Hi All,

I am uploading file using the following code

import java.io.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletInputStream;

public class FileUploadBean {

public void doUpload(HttpServletRequest request) throws

IOException {

PrintWriter pw = new PrintWriter(

new BufferedWriter(new FileWriter("Demo.out")));

ServletInputStream in = request.getInputStream();

int i = in.read();

while (i != -1) {

pw.print((char) i);

i = in.read();

}

pw.close();

}

}

my file gets uploaded properly but hte problem is that it's size is 0(zero).

can anyone help me out?

Or nay other cod eto upload file in java.

Regards,

Anil

[836 byte] By [RAMJANEa] at [2007-11-27 9:53:21]
# 1
Reply to Ramjane....hi , i am too working on this, we will work together...i too have done some codings......we will finish off soonnnnnnnnreply would be appreciated....
senthil_yogaa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
You need the contentLength from the HttpServletRequest.Although I recommend you to use the existing file upload solutions instead of reinventing the wheel.The apache commons fileupload is a nice one.
BalusCa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
thankx for reply.from where i can get that one.Any url?Regards,Anil
RAMJANEa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
I don't stop you to just open google.com and enter "apache commons fileupload" as search keywords :)
BalusCa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
actually i want to upload image and sound files.can i use the same code to both files?
RAMJANEa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Why not? Files are actually just arrays of bytes.
BalusCa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
i am not getting proper code there.help me.
RAMJANEa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
can you tell me why i am getting value -1 here int i = in.read();I think this is the reason for the size of uploaded files for zero.thanks
RAMJANEa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

I'll repeat myself:

> You need the contentLength from the

> HttpServletRequest.

The contentLength *is* the length of the inputStream.

And I'll repeat myself once more:

> Although I recommend you to use the existing file

> upload solutions instead of reinventing the wheel.

> The apache commons fileupload is a nice one.

As it look like that you cannot find the apache commons fileupload (despite of it is the 1st google hit), here is a link:

http://jakarta.apache.org/commons/fileupload/

BalusCa at 2007-7-13 0:22:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...