Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

uploading of a file using servlet

843841Nov 5 2005 — edited Apr 4 2007
Hi,

I have written a code to upload a file from a local client machine to a remote server using servlets.
Here is a code.


package mypackage.com.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.Object;
import java.util.*;
import java.lang.String;
import com.oreilly.servlet.MultipartRequest;

public class FileUpload extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse res)throws
ServletException, IOException{

MultipartRequest multi=new MultipartRequest(req);
String file="file1";
byte[] b=file.getBytes();
InputStream in=null;
BufferedInputStream bis=null;
FileWriter fw=null;

try{

in=multi.getInputStream("file1");
bis=new BufferedInputStream(in);
File output=new File("/fileuploadtest");
fw=new FileWriter(output);

int i;
i=bis.read();
while (i != -1) {
fw.write(i);
i = bis.read();
}
}
catch(IOException e){
System.out.println("Exception=" +e);
}
finally{
try{

if(in!=null)
in.close();
if(bis!=null)
bis.close();
if(fw!=null)
fw.close();
}
catch(Exception e){
System.out.println(e);
}
}

The code is not complete , only few lines are here.
Is the code correct?pls let me know.
Also i have written a form .html.
I'm getting a error as,
cannot resolve symbol: class MultipartRequest
I also imported the packge as i have shown it in the code.
Is any other resources should be added?
Can u pls tel me if there are other ways to write a code without using third party packages so that it can work in a fashion similar to ftp.. and the code must be written using servlets only.

Regards
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 2 2007
Added on Nov 5 2005
15 comments
456 views