/**
* 类名称:UploadTest 类描述:创建人:zhang 创建时间:2015年3月13日 下午4:20:57 修改人:zhang
 * 修改时间:2015年3月13日 下午4:20:57 修改备注:
 * 
 * @version
 * 
 */
@Controller
public class UploadTest {
	@RequestMapping(value = "upFile", method = RequestMethod.POST)
	public void upFile(@RequestParam("name") String name,
			@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		String dir = null;
		if (!file.isEmpty()) {
			ServletContext sc = request.getSession().getServletContext();//得到项目的路径
			dir = sc.getRealPath("/upload"); // 设定文件保存的目录
			//
			saveFile01(file, dir);
			// saveFile(file, dir);
		}
		TestInputToString(dir);
		// TestInputSream(dir);
}
	/**
	 * 储存上传文件 spring格式的MultipartFile 用MultipartFile 里面的transferTo 方法
	 * 
	 * @param file
	 * @param dir
	 */
	private void saveFile01(MultipartFile file, String dir) {
		String filename = file.getOriginalFilename(); // 得到上传时的文件名
File targetFile = new File(dir, filename);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
}
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
		}
		System.out.println("upload over. " + filename);
	}
	/**
	 * @param file
	 * @param dir
	 *            用DataoutPueStream 储存 MultipartFile类型的文件,
	 */
	private void saveFile(MultipartFile file, String dir) {
		/*************************************/
		DataInputStream is = null;
		FileOutputStream fos = null;
		try {
			InputStream in = file.getInputStream();
			is = new DataInputStream(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File fil = new File(dir, file.getOriginalFilename());
		if (!fil.exists()) {// 如果文件不存在创建文件
			fil.mkdirs();
		}
		try {
			fos = new FileOutputStream(fil);
			DataOutputStream dos = new DataOutputStream(fos);
			byte[] by = new byte[1024];
			int data1;
			StringBuffer buffer = new StringBuffer();
			while ((data1 = is.read(by)) != -1) {
				dos.write(by, 0, data1);
				buffer.append(data1);
				// printStream.write(data1);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		/*************************************/
	}
	/**
	 * 把输出流的的文件读出来,转化为字符类型 打印出来
	 */
	private void TestInputToString(String dir) {
		BufferedReader br = null;
		FileInputStream fip = null;
		InputStreamReader isr = null;
		try {
			fip = new FileInputStream(new File(dir + "/新建文本文档.txt"));
			String str;
			/*
			 * 使用 readLine打印文本
			 */
			isr = new InputStreamReader(fip);// 把字节流转化为字符流
			br = new BufferedReader(isr);// 使用字符流缓存区 来提高效率
			while ((str = br.readLine()) != null) {
				System.out.println(str);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					isr.close();
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 利用字节把输入流的文件输出来 ByteArrayOutPutSream 是一个数组用来存放字节 数据被写入一个 byte
	 * 数组。缓冲区会随着数据的不断写入而自动增长
	 */
	public void TestInputSream(String dir) {
		FileInputStream fip = null;
		ByteArrayOutputStream ba = null;
		DataInputStream os = null;
		try {
			fip = new FileInputStream(new File(dir + "/新建文本文档.txt"));
			ba = new ByteArrayOutputStream();
			os = new DataInputStream(fip);
			byte[] b = new byte[1024];
			int rs;
			while ((rs = os.read(b)) != -1) {// 从输入流中读取数据到字节组b中
				ba.write(b, 0, rs);// write的方法b是写入的数据,0是偏移量,rs是写入数据的大小
			}
			System.out.println(ba.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (ba != null) {// 关闭输入流,释放资源
				try {
					ba.close();
					os.close();
					ba.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}}}}}
/***************************************/
2.配置文件 spring4-servlet.xml
	<!-- 自动扫描controller bean,把作了注解的类转换为bean -->
	<mvc:annotation-driven />
	<context:component-scan base-package="com.glass.control" />
	<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/" p:suffix=".jsp">
		<property name="order" value="0" />
	</bean>
   <!-- 支持上传文件 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 
原文:http://www.cnblogs.com/guoke-jsp/p/4337949.html