0

I want to develope upload and download file from server.

Upload.html

<form action="/UploadFile/UploadFile" method="POST"
enctype="multipart/form-data">Select a file: <input
type="submit" name="button" /> <input type="file" name="first"></form>

UploadFile.servlet

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String temp = request.getParameter("first");
    System.out.println(temp);
    File origFile = new File(temp);
    FileOutputStream out = new FileOutputStream(request.getContextPath()
            + "pdtImages/" + "FirstFile");
    InputStream ins = new FileInputStream(origFile);
    try {
        System.out.println(request.getContextPath());
        byte[] buf = new byte[1024];
        int len;
        while ((len = ins.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

When I submitted the file I got null pointer error message. I not very familiar with jsp can anybody help me? I want to store the file to the server directory.

2
  • I already answered this in your previous question, how was that insufficient? stackoverflow.com/questions/2930240/… Commented May 29, 2010 at 13:32
  • Ya sorry I can't understand your code. Commented May 30, 2010 at 4:01

1 Answer 1

2

I use Apache commons fileupload for this task.

Sign up to request clarification or add additional context in comments.

4 Comments

I got tried out Apache commons fileupload also i got this java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream error.
The org.apache.commons.io.output.DeferredFileOutputStream is included in commons.apache.org/io commons-io you also need to include this in your classpath. (WEB-INF/lib)
@newbie123 You should accept answers by clicking the green check mark, otherwise they are listed unanswered.
@newbie123 Basically yes but you would have to implement the parts used from commons by yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.