Spring mvc 文件上传到文件夹(转载+心得)

spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方
1.form的enctype=”multipart/form-data” 这个是上传文件必须的
2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 关于文件上传的配置不能少

applicationContext.xml

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
  7. default-lazy-init="true">
  8. <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  9. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false" />
  10. <!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖! -->
  11. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  12. <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
  14. <!-- 支持上传文件  value表示上传文件的大小-->
  15. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  16. <property name="maxUploadSize" value="10000000000000"/></bean>
  17. </beans>

UploadAction.java

[java] view plaincopyprint?

  1. package com.codeif.action;
  2. import java.io.File;
  3. import java.util.Date;
  4. import javax.servlet.http.HttpServletRequest;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.ModelMap;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.multipart.MultipartFile;
  10. @Controller
  11. public class UploadAction {
  12. @RequestMapping(value = "/upload.do")
  13. public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) {
  14. System.out.println("开始");
  15. String path = request.getSession().getServletContext().getRealPath("upload");
  16. String fileName = file.getOriginalFilename();
  17. //        String fileName = new Date().getTime()+".jpg";
  18. System.out.println(path);
  19. File targetFile = new File(path, fileName);
  20. if(!targetFile.exists()){
  21. targetFile.mkdirs();
  22. }
  23. //保存
  24. try {
  25. // file.transferTo(targetFile);
  26. //也可以用这种方式,上面的方式能上传,但是老是报输出流被占用的错误。
  27. SaveFileFromInputStream(
          file.getInputStream(), realPath + filePath, name + "."+ fileType);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
  32. return "result";
  33. }
  34. }

SaveFileFromInputStream()的实现如下:

public void SaveFileFromInputStream(InputStream stream, String path,
   String filename) throws IOException {
   {      //path + "/"+ filename
    FileOutputStream fs = new FileOutputStream(path + "/"+ filename);
          byte[] buffer =new byte[1024*1024];
          int bytesum = 0;
          int byteread = 0;
          while ((byteread=stream.read(buffer))!=-1)
          {
             bytesum+=byteread;
             fs.write(buffer,0,byteread);
             fs.flush();
          }
          fs.close();
          stream.close();     
      }

index.jsp

[html] view plaincopyprint?

  1. <%@ page pageEncoding="utf-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>上传图片</title>
  7. </head>
  8. <body>
  9. <form action="upload.do" method="post" enctype="multipart/form-data">
  10. <input type="file" name="file" /> <input type="submit" value="Submit" /></form>
  11. </body>
  12. </html>
时间: 2024-08-23 07:01:51

Spring mvc 文件上传到文件夹(转载+心得)的相关文章

spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE b

spring mvc(注解)上传文件的简单例子

spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 关于文件上传的配置不能少 大家可以看具体代码如下: web.xml &

spring mvc 批量上传+文件上传

spring mvc 批量上传+文件上传 简单3步走.搞定! 上传文件成功后: 1 上传文件核心方法 public static String saveWebImgFile(MultipartFile imgFile){ String webFilePath = ""; if(imgFile.getSize() > 0 && isImage(imgFile.getContentType())){ FileOutputStream fos = null; try {

SpringMVC实现单文件上传、多文件上传、文件列表显示、文件下载

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+standard.jar 整个相关的包如下: 整个工程目录如下: 二.配置web.xml和SpringMVC文件

spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成目录 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ #fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be

SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程免费下载 一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+standard.jar 整个相关的包如下: 整个工程目录如下: 二.配置web.xml和Spr

关于异步文件上传和文件表单元素的复制、设置和清除

一.解决文件异步上传的方法有3种:1.使用iframe 2.使用FormData(html5新功能)  3.使用flash 1.使用iframe 原来我以为使用iframe是把整个表单复制到iframe里面,然后把iframe里面的表单提交给服务器,这样来实现表单的异步上传.最后我想错了,实现原理比我想的要简单的多.先看一下代码: 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <

struts2文件上传,文件类型 allowedTypes

struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript', 3 '.aif' : 'audio/x-aiff', 4 '.aifc' : 'audio/x-aiff', 5 '.aiff' : 'audio/x-aiff', 6 '.au' : 'audio/basic', 7 '.avi' : 'video/x-msvideo', 8 '.bat'

Javascript Fromdata 与jQuery 实现Ajax文件上传以及文件的删除

前端HTML代码: <!DOCTYPE html> <html> <head> <title>ajax</title> <script type="text/javascript" src="js/jquery.js"></script> <meta charset="utf-8" /> <style type="text/css&qu