一.主要有两个java类,和一般的servlet放在一起即可.
1.FileUploadBean.java
java 代码
1. package chb.demo.web;
2.
3. import org.springframework.web.multipart.MultipartFile;
4.
5. /**
6. * @author chb
7. *
8. */
9. public class FileUploadBean {
10.
11. private MultipartFile file;
12.
13. public void setFile(MultipartFile file) {
14. this.file = file;
15. }
16.
17. public MultipartFile getFile() {
18. return file;
19. }
20.}
2.FileUploadController.java
java 代码
1. package chb.demo.web;
2.
3. import java.io.FileOutputStream;
4. import java.io.IOException;
5. import java.io.InputStream;
6.
7. import javax.servlet.http.HttpServletRequest;
8. import javax.servlet.http.HttpServletResponse;
9.
10.import org.springframework.validation.BindException;
11.import org.springframework.web.multipart.MultipartFile;
12.import org.springframework.web.servlet.ModelAndView;
13.import org.springframework.web.servlet.mvc.SimpleFormController;
14.
15.
16./**
17. * @author chb
18. *
19. */
20.public class FileUploadController extends SimpleFormController {
21.
22. protected ModelAndView onSubmit(
23. HttpServletRequest request,
24. HttpServletResponse response,
25. Object command,
26. BindException errors){
27.
28. try
29. {
30. // cast the bean
31. FileUploadBean bean = (FileUploadBean) command;
32.
33. // let's see if there's content there
34. MultipartFile file = bean.getFile();
35.
36. if (file == null) {
37. throw new Exception("上传失败:文件为�空");
38. }
39. if(file.getSize()>10000000)
40. {
41. throw new Exception("上传失败:文件大小不能超过10M");
42. }
43. //得到文件�名
44. String filename=file.getOriginalFilename();
45.
46. if(file.getSize()>0){
47. try {
48. SaveFileFromInputStream(file.getInputStream(),"D:/",filename);
49. } catch (IOException e) {
50. System.out.println(e.getMessage());
51. return null;
52. }
53. }
54. else{
55. throw new Exception("上传失败:上传文件不能为�空");
56. }
57. // well, let's do nothing with the bean for now and return:
58. try {
59. return super.onSubmit(request, response, command, errors);
60.
61. } catch (Exception e) {
62. System.out.println(e.getMessage());
63. return null;
64. }
65. }
66. catch(Exception ex)
67. {
68. System.out.println(ex.getMessage());
69. return null;
70. }
71. }
72.
73. /**保存文件
74. * @param stream
75. * @param path
76. * @param filename
77. * @throws IOException
78. */
79. public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException
80. {
81. FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
82. byte[] buffer =new byte[1024*1024];
83. int bytesum = 0;
84. int byteread = 0;
85. while ((byteread=stream.read(buffer))!=-1)
86. {
87. bytesum+=byteread;
88. fs.write(buffer,0,byteread);
89. fs.flush();
90. }
91. fs.close();
92. stream.close();
93. }
94.}
95.
二.配置文件中如下配置:
1.web.xml,利用spring mvc模式,大家应该都很熟悉了
xml 代码
1. <servlet>
2. <servlet-name>chb</servlet-name>
3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4. <load-on-startup>1</load-on-startup>
5. </servlet>
6.
7. <servlet-mapping>
8. <servlet-name>chb</servlet-name>
9. <url-pattern>*.do</url-pattern>
10.</servlet-mapping>
2.chb-servlet.xml,这里要配置映射,并可以设定最大可上传文件的大小
xml 代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <!-- Multi-Action 用来标识method的变量名定义-->
5. <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
6. <property name="paramName">
7. <value>action</value>
8. </property>
9. <property name="defaultMethodName">
10. <value>index</value>
11. </property>
12. </bean>
13.
14. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
15. <!-- one of the properties available; the maximum file size in bytes -->
16. <property name="maxUploadSize" value="10000000"/>
17. </bean>
18.
19.
20. <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
21. <property name="mappings">
22. <props>
23. <prop key="/upload.do">fileUploadController</prop>
24. </props>
25. </property>
26. </bean>
27.
28. <bean id="fileUploadController" class="chb.demo.web.FileUploadController">
29. <property name="commandClass" value="chb.demo.web.FileUploadBean"/>
30. <!-- 上传失败时跳转页面 -->
31. <property name="formView" value="/user/err.jsp"/>
32. <!-- 上传成功时跳转页面 -->
33. <property name="successView" value="/user/confirmation.jsp"/>
34. </bean>
35.</beans>
三.设定jsp页面
xml 代码
1. <form id="form1" method="post" action="upload.do" enctype="multipart/form-data">
2. <tr>
3. <td width="25%" align="right">上传文件:</td>
4. <td><input id="file" type="file" NAME="file" style="width:300px;"></td>
5. </tr>
6. <tr align="center" valign="middle">
7. <td height="60" colspan="2"><input type="submit" ID="BtnOK" value="确认上传"></td>
8. </tr>
9. </form>
ok,现在就可以上传文件了,挺简单吧?这里我只列出了基本步骤,至于具体的操作(比如中文问题)可能就需要大家自己再完善完善了