SpringBoot上传下载

2023-12-25 16:26:22 浏览数 (1)

额外加入的依赖

代码语言:javascript复制
<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

上传

上传页面:注意上传域的name属性要和controller参数名一致,enctype属性

代码语言:javascript复制
<form method="POST" enctype="multipart/form-data" action="fileUpLoad.action">
		<p>
			文件:<input type="file" name="file" />
		</p>
		<p>
			<input type="submit" value="上传" />
		</p>
	</form>

上传参数控制

代码语言:javascript复制
package com.example.demo.config;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 配置上传
 * @author 柴火
 *
 */
@Configuration
public class FileUpLoadConfig {
	
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory(); 
		 设置文件大小限制,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;
		factory.setMaxFileSize("128KB"); // KB,MB 
		/// 设置总上传数据总大小
		factory.setMaxRequestSize("256KB"); 
		// Sets the directory location where files will be stored. 设置上传路径
		//factory.setLocation("路径地址");
		return factory.createMultipartConfig();
	}
}

controller代码(2种上传方式)

代码语言:javascript复制
package com.example.demo.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UpLoadController {

	@RequestMapping("/fileUpLoad.action")
	@ResponseBody
	public String upLoadFile(MultipartFile file) throws IllegalStateException, IOException {
		BufferedOutputStream stream = null;
		if (!file.isEmpty()) {

			
			String path = "E:/stsWorkSpace/SpringBoot_Test/src/main/resources/upload";
			File newFile=new File(path "/" file.getOriginalFilename());
			if(!newFile.exists()){
				newFile.createNewFile();
			}
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile)); 
			out.write(file.getBytes()); 
			out.flush(); 
			out.close(); 
			
			

			return "上传成功";
		}
		return "上传失败";
	}

}
代码语言:javascript复制
package com.example.demo.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UpLoadController {

	@RequestMapping("/fileUpLoad.action")
	@ResponseBody
	public String upLoadFile(MultipartFile file) throws IllegalStateException, IOException {
		BufferedOutputStream stream = null;
		if (!file.isEmpty()) {

			
			// 打印文件的名称
			System.out.println("FileName:"   file.getOriginalFilename());
			// 确定上传文件的位置
			String path = "E:/stsWorkSpace/SpringBoot_Test/src/main/resources/upload";
			
			File newFile = new File(path, file.getOriginalFilename());
			//如果不存在,创建一哥副本
			if (!newFile.exists()) {
				newFile.createNewFile();
		
			}
				
			//将io上传到副本中
			file.transferTo(newFile);
	        		 		

			return "上传成功";
		}
		return "上传失败";
	}

}

下载

代码语言:javascript复制
package com.example.demo.controller;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DownLoadController {
	
	@RequestMapping("/fileDownLoad.action")
	public ResponseEntity<byte[]> textFileDownload() throws Exception {
	
		String path = "E:/WorkSpace1/SpringMVC_001/WebContent/WEB-INF/fileupload/1.jpg";
		File file = new File(path);
		HttpHeaders headers = new HttpHeaders();
		// String fileName=new
		// String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
		String fileName = new String(file.getName().getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱码问题
		headers.setContentDispositionFormData("attachment", fileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
		
		
	}

}

0 人点赞