前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot上传下载

SpringBoot上传下载

作者头像
CBeann
发布2023-12-25 16:26:22
1410
发布2023-12-25 16:26:22
举报
文章被收录于专栏:CBeann的博客CBeann的博客

额外加入的依赖

代码语言: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);
		
		
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 上传
    • 上传页面:注意上传域的name属性要和controller参数名一致,enctype属性
      • 上传参数控制
        • controller代码(2种上传方式)
        • 下载
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档