标签:http os java io ar for 文件 art cti
springmvc配置文件:
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" > <!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!--文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="104857600" /> <property name="maxInMemorySize" value="4096" /> <property name="defaultEncoding" value="UTF-8"></property> </bean> <mvc:annotation-driven /> <!-- 处理器 --> <!-- <bean name="/hello" class="com.mvc.jn.controller.HelloWorldController"/> --> <context:component-scan base-package="com"/> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="<%=request.getContextPath()%>/jquery/jquery-1.4.4.js"></script> </head> <body> <form action="fileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="upload"> </form> </body> </html>
package com.lin.controller; import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import com.lin.model.StudentEntity; @Controller @RequestMapping(value = "/") public class MainController { /* * 通过流的方式上传文件 * * @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象 */ @RequestMapping("fileUpload") public String fileUpload(@RequestParam("file") CommonsMultipartFile file) throws IOException { // 用来检测程序运行时间 long startTime = System.currentTimeMillis(); System.out.println("fileName:" + file.getOriginalFilename()); try { // 获取输出流 OutputStream os = new FileOutputStream("D:/" + new Date().getTime() + file.getOriginalFilename()); // 获取输入流 CommonsMultipartFile 中可以直接得到文件的流 InputStream is = file.getInputStream(); BufferedInputStream buf = new BufferedInputStream(is); byte[] b = new byte[1024]; while (buf.read(b) != (-1)) { os.write(b, 0, b.length); } os.flush(); os.close(); is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } long endTime = System.currentTimeMillis(); System.out.println("方法一的运行时间:" + String.valueOf(endTime - startTime) + "ms"); return "success"; } /* * 采用file.Transto 来保存上传的文件 */ @RequestMapping("fileUpload2") public String fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException { long startTime = System.currentTimeMillis(); System.out.println("fileName:" + file.getOriginalFilename()); String path = "D:/" + new Date().getTime() + file.getOriginalFilename(); File newFile = new File(path); // 通过CommonsMultipartFile的方法直接写文件(注意这个时候) file.transferTo(newFile); long endTime = System.currentTimeMillis(); System.out.println("方法二的运行时间:" + String.valueOf(endTime - startTime) + "ms"); return "success"; } /* * 采用spring提供的上传文件的方法 */ @RequestMapping("springUpload") public String springUpload(HttpServletRequest request) throws IllegalStateException, IOException { long startTime = System.currentTimeMillis(); // 将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); // 检查form中是否有enctype="multipart/form-data" if (multipartResolver.isMultipart(request)) { // 将request变成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 获取multiRequest 中所有的文件名 Iterator iter = multiRequest.getFileNames(); while (iter.hasNext()) { // 一次遍历所有文件 MultipartFile file = multiRequest.getFile(iter.next() .toString()); if (file != null) { String path = "D:/" + file.getOriginalFilename(); // 上传 file.transferTo(new File(path)); } } } long endTime = System.currentTimeMillis(); System.out.println("方法三的运行时间:" + String.valueOf(endTime - startTime) + "ms"); return "success"; } }
标签:http os java io ar for 文件 art cti
原文地址:http://blog.csdn.net/hackcoder/article/details/38901397