读书人

springmvc 下传文件

发布时间: 2012-10-24 14:15:58 作者: rapoo

springmvc 上传文件

采用springmvc来上传文件,时间上挺简单的,但是由于路径没有配置好,以后自己不够坚持,总是出现错误。

在这里记录下步骤,以后就不会再出现这样的错误了

首先建立项目,加入jar包,
springmvc 下传文件
?这个项目中加入了图上的这些jar,也可根据启动tomcat时报的异常时来添加所需要的jar

2、接着配置web.xml,配置如下:

<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>

3、由于没有指定spring mvc的处理文件,所以用默认的,在web.xml同级目录下建立dispatcher-servlet.xml

配置入下:

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!--注解驱动器-->    <!-- <mvc:annotation-driven/> --><!--  启动Spring MVC的注解功能,完成请求和注解POJO的映射  --><bean /><!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖! -->    <bean />     <!--扫描组件,扫描那个目录下的文件-->    <context:component-scan base-package="com.demo.controllers"/>    <!--分段文件解析器-->    <bean id="multipartResolver" p:defaultEncoding="UTF-8"/>    <!--视图解析器 jsp-->    <bean id="viewResolver" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/page/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

?

4、接着写jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java"%><html><head><title>Upload a file please</title></head><body><form method="post" action="upload.do"enctype="multipart/form-data"><input type="file"name="file" /><input type="submit" /></form></body></html>
?

5、接下来写控制层:

@Controllerpublic class FileUploadController{private Logger logger = LoggerFactory.getLogger(FileUploadController.class);@RequestMapping("/oneFileUpload")    //跳到相应的jsp页面public String handleFormUpload(){return "oneFileUpload";}/** * 单文件上传 * @param file 文件 * @return * @throws IOException */@RequestMapping(value = "/upload", method = RequestMethod.POST)public ModelAndView han(@RequestParam("file") CommonsMultipartFile file)throws Exception{if(file == null){return new ModelAndView("oneFileUpload");}else{this.copyFile(file.getInputStream(), file.getOriginalFilename());}return new ModelAndView("success");}  /**   * 写文件到本地   * @param in   * @param fileName   * @throws IOException   */  private void copyFile(InputStream in,String fileName) throws IOException{  FileOutputStream fs = new FileOutputStream("F:/upload/"+ fileName);byte[] buffer = new byte[1024 * 1024];int bytesum = 0;int byteread = 0;while ((byteread = in.read(buffer)) != -1) {bytesum += byteread;fs.write(buffer, 0, byteread);fs.flush();}fs.close();in.close();  } }
?

发布工程,启动tomcat,输入:http://localhost:8080/工程名/oneFileUpload.do,就会进入到oneFileUpload.jsp页面中,
springmvc 下传文件
?选择要上传的文件,然后提交,就会成功了

?

?

?

读书人网 >VC/MFC

热点推荐