使用SpringMVC上传文件
2018-07-20 来源:open-open
package com.foo.controller; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @Controller @RequestMapping(value="/file") public class FileUpload{ @RequestMapping(value="/gotofilepage") public String gotofilepage(){ System.out.println("进入到file中的gotofilepage方法中。。。。"); return "uploadfile"; } @RequestMapping(value="/fileupload") public void fileUpload(HttpServletRequest request, HttpServletResponse response) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH"); /** 得到图片保存目录的真实路径 **/ String realpath = request.getSession().getServletContext().getRealPath("/"); /** 构建图片保存的目录 **/ String filedir ="files" + File.separator + dateformat.format(new Date()); String filelocationdir = realpath + filedir; /** 根据真实路径创建目录 **/ File logoSaveFile = new File(filelocationdir); if (!logoSaveFile.exists()) logoSaveFile.mkdirs(); /** 页面控件的文件流 **/ MultipartFile multipartFile = multipartRequest.getFile("file"); /** 获取文件的后缀,防止别人传可执行文件 **/ String suffix = multipartFile.getOriginalFilename().substring( multipartFile.getOriginalFilename().lastIndexOf(".")); String imagename = multipartFile.getOriginalFilename(); /** 拼成完整的文件保存路径加文件 **/ String filename = filelocationdir + File.separator + imagename; File file = new File(filename); try { multipartFile.transferTo(file); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
uploadfile.jsp
<body> <h1>文件上传</h1> <form action="./fileupload.action" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="upload" /> </form> </body>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- @Controller, @Service, @Configuration, etc. --> <context:component-scan base-package="com.foo.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 上传文件必须加 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set the max upload size100MB --> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> <!-- <task:annotation-driven/> <bean id="taskTest" class="com.htf.task.MyTask"></bean> <task:scheduled-tasks> <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" /> </task:scheduled-tasks> <context:component-scan base-package="com.htf.task" /> --> </beans>
需要jar
springmvc包
commons-io-2.2.jar
commons-fileupload-1.3.1.jar
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:java 调用mysql存储过程
最新资讯
热门推荐