Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

java - How to receive a file type parameter from html/jsp into a servlet

I want to take image as input into my web page. I have written following code in my jsp for this :-

<form action="Upload" method="get" enctype="multipart/form-data">
    Image<input type="file" name="image" accept="image/jpg" id="image">
    <input type="submit" value="submit">
</form>

but I do not know how to receive the "image" parameter in a servlet that is whether it should be a input stream or file, I have no idea. Please tell me the correct code for it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use Apache Commons File. The form method must be method="POST". Then in your web.xml you need to map the request to your servlet:

<servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.stackoverflow.MyServletClass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/Upload</url-pattern>
</servlet-mapping>

Then you write a class that extends HttpServlet and implement to doPost() method.

well, just go here: http://www.codejava.net/java-ee/servlet/apache-commons-fileupload-example-with-servlet-and-jsp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...