0

I was making a web application using jsp and servlets.In a form i tried to upload a file and enter some value in a textbox.Here is my form :

<FORM METHOD=POST ACTION="sharingfile" NAME="form1" id="form1" enctype="multipart/form-data">   
<INPUT TYPE="file" NAME="file" value="file">  
<INPUT TYPE="text" NAME="totalshares" size="20">  
<INPUT TYPE="submit" value="NEXT"> </FORM>  

And in my sharingfile servlet I wrote following code :

        String N = request.getParameter("totalshares"); 
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        ArrayList<String>filenamess=new ArrayList<String>();
        List<FileItem> files = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        out.println(files.size());
        Iterator it=    files.iterator();
        while(it.hasNext()){

        FileItem fi=(FileItem)it.next();

        if (fi.isFormField()) {
            continue;
        }               
        filenamess.add(fi.getName());
        //System.out.println("filename: " + fi.getName());
        InputStream is= fi.getInputStream();
        FileOutputStream fos=new FileOutputStream("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\SharedDocuments\\"+fi.getName());
        int x=is.read();

        while(x>=0){
            fos.write((byte)x);
            x=is.read();
            System.out.println("reading");
            }

        }
        out.println(N);
        out.println(filenamess.get(0));

But it give value of N as NULL.What can be the reason ?How to resolve it ?

1

1 Answer 1

0

You cant the values of the other parameters simply if you use your form type as enctype="multipart/form-data" . you need to iterate like this

For a regular form field, you will most likely be interested only in the name of the item, and its String value. As you might expect, accessing these is very simple.

// Process a regular form field
if (files.isFormField()) {
    String N=request.getParameter("totalshares");
}

For file related ,

For a file upload, there are several different things you might want to know before you process the content. Here is an example of some of the methods you might be interested in.

// Process a file upload
if (!files.isFormField()) {
    String fieldName = item.getFieldName();
    String fileName = item.getName();
    String contentType = item.getContentType();
    boolean isInMemory = item.isInMemory();
    long sizeInBytes = item.getSize();
    ...
}

Hope this helps !

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.