Upload Client File to Server using Java Servlet

H

hust6

I have a Java Servlet that produces an HTML form. It asks the user to
input a file using standard input tags:

<input type="file" size=75 name=fileInput>

Once the form is submitted, it calls a different servlet that processes
all the data. The only problem I am having is saving the client's file
on the server. A FileNotFound exception is thrown when the servlet
tries to save the file in a new location. Here is a snippet of my
code:


File newFile = new File(serverFileLoc, fileName);
temp = req.getParameterValues("fileInput");
File origFile = new File(temp);

InputStream ins = new FileInputStream(origFile);
OutputStream outs = new FileOutputStream(newFile);

byte[] buf = new byte[1024];
int len;
while ((len = ins.read(buf)) > 0)
{
outs.write(buf, 0, len);
}
ins.close();
outs.close();


Everything works fine when run locally, but when run from a client
machine, it fails when I create the FileInputStream due to a
FileNotFoundException.

Any ideas?

I would greatly appreciate anyone that could help me solve this
problem. Thanks, Matt
 
V

Vincent van Beveren

Any ideas?

What package do you use to process the upload? CommonsUpload, COS?
Home-made solution? The default servlet spec does not handle mutlipart
uploads, so you'll have to use a 3rd party library to make it work.

Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top