1

I need to implement a servlet that uploads files to a server, I realize everyone says it has to be a POST method in regard to uploading files and not with GET method. However is there a way to upload a file and have the parameters of the request show up in the url even if the request is coming from POST method? If not, is there another approach?

Currently my servlet using post method is http://example.com/FileUpload/UploadFile

What I want is somehting like http://example.com/FileUpload/UploadFile?id=125&fileNum=5

Thanks for your input.

1
  • 1
    POST is the recommended way to send files to a server; one reason is the limited length of GET parameters.
    – miku
    Commented Feb 16, 2011 at 17:59

4 Answers 4

2

Simply POST to

http://example.com/FileUpload/UploadFile?id=125&fileNum=5

instead of

http://example.com/FileUpload/UploadFile

There is no such restriction that you cannot post to an URL having parameters. You can process the post data as you are doing now, plus, you can get the get parameters also.

2
  • do you have a link or more documentation for me to learn how to do it this way?
    – David
    Commented Feb 16, 2011 at 18:14
  • Which part is not clear to you? How do you do it currently? And, how are you planning to get the id and fileNum values? Commented Feb 17, 2011 at 4:14
1

I think it would not be an elegant solution, but you could use JavaScript to alter the action of the form element before submitting it to include querystring parameters.

The form will be something like:

<form method="POST" id="myForm" onSubmit="submitMyForm(this)>
   <input type="text" id="id">

Then you will need JavaScript to change the action element of the form:

function submitMyForm(theForm) {
  theForm.action="http://example.com/FileUpload/UploadFile?id=" + 
         getElementById("id").value;
  theForm.submit();
}

Is there some reason you cannot just submit the parameters with post and pull them out on the server side?

1
  • "Is there some reason you cannot just submit the parameters with post and pull them out on the server side?" I will have multiple clients (non-gui) that will use the url to upload file. So I thought a good way would to put the file id or what have you in the url.
    – David
    Commented Feb 16, 2011 at 18:10
0

Alternatively, if you do a multipart/form-data post you can include multiple parameters along with your file. The parameters are sent as part of the post body, along with the file.

0

You can send parameters and files in the POST. For example in the html you can have a form with this values, they can be of hidden type. In the servlet you can get the values in the same way you do using the GET.

It is also better to use the POST method because the user can't change the value in the URL direction bar.

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.