开发者

java - how to get data of a file from a form?

开发者 https://www.devze.com 2023-03-28 18:48 出处:网络
I\'ve created a form and I need the user to enter some info then upload a picture. So lets say I have something like this:

I've created a form and I need the user to enter some info then upload a picture.

So lets say I have something like this:

<form method="post" enctype="multipart/form-data" action="some servlet/filter">
<input type="file" name="logo">
</form>

I need to use java to change that data to a byte[] then to blob so I can insert to a table. How do I get the data from this form?

A bit of info on this: I created the page using javascript, then when submitted it will call a java function to handle the data from the form. It seem开发者_StackOverflow社区s that when I submit the form the data for the file is not passed over to the servlet.

So far the few methods I've tried have returned null and I'm outta ideas...

Any examples/help is greatly appreciated!

I think the main question I have is where is the file data stored before the java file start working on it? Is a special global variable holding the data or something like that?


You can use the Apache Commons FileUpload library.

The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.

FileUpload parses HTTP requests which conform to RFC 1867, "Form-based File Upload in HTML". That is, if an HTTP request is submitted using the POST method, and with a content type of "multipart/form-data", then FileUpload can parse that request, and make the results available in a manner easily used by the caller.


If I understood you right, you need something similar to this example:

http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html


I used the following tutorial one year ago:

http://balusc.blogspot.com/2009/12/uploading-files-in-servlet-30.html

It looks like it's a lot, but it's actually easy to understand, and it has a lot of good information.

0

精彩评论

暂无评论...
验证码 换一张
取 消