springboot文件上传大小限制(springboot 上传文件大小)

springboot文件上传大小限制(springboot 上传文件大小)

一、HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>UploadFile</title>
</head>
<body>
<form method="post" action="http://localhost:8083/upload" enctype="multipart/form-data">
   <input type="file" name="file">
   <br><br>
   <input type="submit" value="提交">
</form>
</body>
</html>

二、Service

package com.gl.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import net.sf.json.JSONObject;

@Service
public class UploadService {
public JSONObject fileUpload(MultipartFile file) throws Exception {
JSONObject result = new JSONObject();
if (file == null || file.isEmpty()) {
result.put("msg", "未选择需上传的文件");
return result;
}
// 取得绝对路径
String filePath = new File("uploadFiles").getAbsolutePath();
// 取得相对路径
String pathString = new File("uploadFiles").getPath();
File fileUpload = new File(filePath);
if (!fileUpload.exists()) {
fileUpload.mkdirs();
}

// 获取原文件名
fileUpload = new File(filePath, file.getOriginalFilename());
if (fileUpload.exists()) {
result.put("msg", "上传的文件已存在");
return result;
}

try {
String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + ext;
file.transferTo(new File(filePath, newFileName));
// 输出相对路径和新文件名,测试用
System.out.println(pathString + newFileName);
result.put("success", true);
return result;
} catch (IOException e) {
result.put("failed", false);
result.put("msg", "上传日志文件到服务器失败:" + e.toString());
return result;
}
}

/*
 * 下载服务 File file=new File(); 这句是新建一个文件。file.separator这个代表系统目录中的间隔符,
 * 说白了就是斜线,不过有时候需要双线,有时候是单线, 你用这个静态变量就解决兼容问题了。
 */
public void fileDownload(String name, HttpServletResponse response) throws Exception {
File file = new File("uploadFiles" + File.separator + name);
JSONObject result = new JSONObject();
if (!file.exists()) {
result.put("msg", name + "文件不存在存在");
}
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + name);

byte[] buffer = new byte[1024];
try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis)) {

OutputStream os = response.getOutputStream();

int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
}
}

}

三、Controller

package com.gl.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gl.service.UploadService;

import net.sf.json.JSONObject;

@Controller
public class UploadController {

@Resource
UploadService uploadService;

@ResponseBody
@PostMapping("/upload")
public JSONObject logUpload(@RequestParam("file") MultipartFile file) throws Exception {
return uploadService.fileUpload(file);
}

/*
 * 下载文件
 */
@GetMapping(value = "/download/{name}")
public void logDownload(@PathVariable String name, HttpServletResponse response) throws Exception {
uploadService.fileDownload(name, response);
}
}

四、application.properties限制文件大小

#限制上传文件大小
#max-file-size是设置单个文件的大小
#max-request-size是设置单次请求的文件的总大小
#如果是想要不限制文件上传的大小,那么就把两个值都设置为-1
spring.servlet.multipart.max-file-size = 1MB
spring.servlet.multipart.max-request-size = 150MB
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论