方式一:利用commons下面的fileupload进行上传
步骤1:添加依赖
- <dependency>
- <groupId>com.alibaba.external</groupId>
- <artifactId>jakarta.commons.fileupload</artifactId>
- <version>1.2.1</version>
- </dependency>
步骤2:在jsp页面中添加上传文件,使用enctype="multipart/form-data"
- <form method="post" action="/FileUploadServlet" enctype="multipart/form-data">
- fileupload:<input name="filename" type="file"> <br>
- name:<input name="name" type="text" value="zhangsan"><br>
- <input name="sub" type="submit" value="提交">
- </form>
步骤3:在servlet中处理文件上传,参考官方文档:http://commons.apache.org/fileupload/using.html
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- try {
- List items = upload.parseRequest(request);
- Iterator iter = items.iterator();
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (item.isFormField()) {
- System.out.println("isFormField----" + item.getName());
- } else {
- String fullPathName = item.getName();
- String filename = this.getServletContext().getRealPath(
- "upload")
- + File.separator
- + new Date().getTime()
- + fullPathName.substring(fullPathName
- .lastIndexOf("."));
- File file = new File(filename);
- try {
- item.write(file);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- request.getRequestDispatcher("pages/s5_result.jsp").forward(
- request, response);
- } catch (FileUploadException e) {
- e.printStackTrace();
- }
步骤4:在web.xml中添加servlet mapping
- <servlet>
- <servlet-name>FileUploadServlet</servlet-name>
- <servlet-class>com.alibaba.upload.FileUploadServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>FileUploadServlet</servlet-name>
- <url-pattern>/FileUploadServlet</url-pattern>
- </servlet-mapping>
方式2:使用jquery的ajaxfileupload插件怎么实现异步上传:AjaxFileUpload
页面代码:
- <html>
- <!-- 引入相关的js文件,相对路径 -->
- <script type="text/javascript" src="js/jquery.js"></script>
- <script type="text/javascript" src="js/ajaxfileupload.js"></script>
- <!-- 执行上传文件操作的函数 -->
- <script type="text/javascript">
- function ajaxFileUpload(){
- $.ajaxFileUpload(
- {
- url:'update.do?method=uploader', //需要链接到服务器地址
- secureuri:false,
- fileElementId:'houseMaps', //文件选择框的id属性
- dataType: 'xml', //服务器返回的格式,可以是json
- success: function (data, status) //相当于java中try语句块的用法
- {
- $('#result').html('添加成功');
- },
- error: function (data, status, e) //相当于java中catch语句块的用法
- {
- $('#result').html('添加失败');
- }
- }
- );
- }
- </script>
- </head>
- <body>
- <form method="post" action="update.do?method=uploader" enctype="multipart/form-data">
- <input type="file" id="houseMaps" name="houseMaps"/>
- <input type="button" value="提交" onclick="ajaxFileUpload()"/>
- </form>
- <div id="result"></div>
- </body>
- </html>
服务器代码:
- public class UpdateAction extends DispatchAction {
- public ActionForward uploader(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- UpFormForm upFormForm = (UpFormForm) form;
- FormFile ff = upFormForm.getHouseMaps();
- try {
- InputStream is = ff.getInputStream();
- File file = new File("D:/" + ff.getFileName()); //指定文件存储的路径和文件名
- OutputStream os = new FileOutputStream(file);
- byte[] b = new byte[1024];
- int len = 0;
- while((len = is.read(b)) != -1){
- os.write(b, 0, len);
- }
- os.close();
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/678115,如需转载请自行联系原作者