SpringMVC的文件上传以及下载、MultipartResolver

12 篇文章 0 订阅
3 篇文章 0 订阅
需要导入jar包

commons-fileupload-1.2.1.jar
commons-io-2.0.jar

配置文件上传解析器
<!-- 
        	解析器,将接收到客户端所上传的文件的File对象转换成MultipartFile对象
        	注意:文件解析器的beanid必须是multipartResolver,因为在解析的过程中底层的代码会通过getBean方法来获取此bean
        -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--         	 设置文件及解析的编码,一定要与页面的pageencoding保持一致 -->
        	<property name="defaultEncoding" value="utf-8"></property>
        	<!-- 设置做大上传文件的大小 -->
        	<property name="maxUploadSize" value="888888888"></property>
        </bean>
上传页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="down">下载图片</a></br>
	<form action="up" method="post" enctype="multipart/form-data">
		头像:<input type="file"  name="uploadFile"/>
		描述:<input type="text"  name="desc"/>
		<input type="submit" value="上传">
	</form>
</body>
</html>
Controller层代码
package com.blh.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class TestUploadAndDownloadController {
	@RequestMapping("/down")
	public ResponseEntity<byte[]> down(HttpSession session) throws IOException{
		//获取下载文件的路径
		String realPath = session.getServletContext().getRealPath("imgs");
		String finalPath = realPath + File.separator+"1.jpg";
		InputStream is = new FileInputStream(finalPath);
		//available()获取输入流所读取的文件的最大字节数
		byte[] b = new byte[is.available()];
		is.read(b);
		//设置响应头
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Disposition", "attachment;filename=abc.jpg");
		//设置状态码
		HttpStatus statusCode = HttpStatus.OK;
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(b,headers,statusCode);
		System.out.println(realPath);
		is.close();
		return entity;
	}
	
	@RequestMapping(value="/up_old",method = RequestMethod.POST)
	public String up_old(String desc,MultipartFile uploadFile,HttpSession session) throws IOException {
		String name = uploadFile.getName();
		//获取上传文件的名称
		String originalFilename = uploadFile.getOriginalFilename();
		String path = session.getServletContext().getRealPath("photoes")+File.separator+originalFilename;
		//获取输入流
		InputStream is = uploadFile.getInputStream();
		//获取输出流
		File file = new File(path);
		OutputStream os = new FileOutputStream(file);
		int i = 0;
//		while((i = is.read()) != -1) {
//			os.write(i);
//		}
		byte[] b = new byte[1024];
		while((i = is.read(b)) != -1) {
			os.write(b, 0, b.length);
		}
		os.close();
		is.close();
		return "success";
	}
	
	@RequestMapping(value="/up",method = RequestMethod.POST)
	public String up(String desc,MultipartFile uploadFile,HttpSession session) throws IOException {
		//获取上传文件的名称
		String originalFilename = uploadFile.getOriginalFilename();
		String finaFileNameString = UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));
		String path = session.getServletContext().getRealPath("photoes")+File.separator+finaFileNameString;
		File file = new File(path);
		uploadFile.transferTo(file);
		return "success";
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值