Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...