当前位置:网站首页>简单实现文件上传
简单实现文件上传
2022-06-10 15:46:00 【Java-请多指教】
SpirigBoot项目
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>主类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UploadTestApplication {
public static void main(String[] args) {
SpringApplication.run(UploadTestApplication.class, args);
}
}
结果集类:
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Resp<E> {
private String code;
private String message;
private E body;
public static<E> Resp<E> success(E body){
return new Resp("200","上传成功",body);
}
public static<E> Resp<E> fail(String code,String message){
return new Resp(code,message,(Object)null);
}
}
接口
import com.example.uploadtest.entity.Resp;
import org.springframework.web.multipart.MultipartFile;
public interface UploadService {
public Resp<String> upload(MultipartFile file);
}
接口实现类
import com.example.uploadtest.entity.Resp;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
@Service
public class UploadServiceImpl implements UploadService{
@Override
public Resp<String> upload(MultipartFile file) {
if(file.isEmpty()) return Resp.fail("400","文件为空!");
//得到文件的原始名字
String originalFilename = file.getOriginalFilename();
//根据时间毫秒数取一个新的文件名称
String fileName = System.currentTimeMillis() + "." + originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
String filePath="G:\\uploadtest\\";
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) dest.getParentFile().mkdirs();
try {
file.transferTo(dest);
} catch (Exception e) {
e.printStackTrace();
Resp.fail("500",originalFilename+"上传失败!");
}
return Resp.success(fileName);
}
}
Controllerc控制层
import com.example.uploadtest.entity.Resp;
import com.example.uploadtest.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class UpLoadController {
@Autowired
private UploadService uploadService;
@RequestMapping(value = "/upload.do",method = RequestMethod.POST)
public Resp<String> upload(@RequestParam("file") MultipartFile file){
return uploadService.upload(file);
}
}
边栏推荐
- Thinking and precipitation after docking with hundreds of third-party APIs
- 软件测试架构师,给后辈的16条忠告,快看看别错过
- Devops-1- introduction
- 【第14节 STL容器二】
- Distribution aware coordinate representation for human pose estimation
- Matlab learning notes (3) advanced operation of MATLAB matrix
- Rk3308 key and LED light
- torch.utils.data.DataLoader()详解【Pytorch入门手册】
- Why do I need a thread pool? What is pooling technology?
- RK3308--固件编译
猜你喜欢

Swift read userinfo of remote notification

技术分享| 快对讲,全球对讲

I used Matlab to reproduce the trembling sonic boom Fire Games Apple snake
![Jerry's ble dynamic power regulation [chapter]](/img/29/22be6dca25c4e6502f076fee73dd44.png)
Jerry's ble dynamic power regulation [chapter]

STM32 printf garbled

Devops-2- from the Phoenix Project

Diagram of the quarterly report of station B: the revenue is RMB 5.1 billion, with a year-on-year increase of 30% and nearly 300million monthly active users

安霸CV2FS/CV22FS获得ASIL C芯片功能安全认证,超越市场同类芯片水平

Why do I need a thread pool? What is pooling technology?

Nerf: the popularity of using deep learning to complete 3D rendering tasks
随机推荐
Distribution aware coordinate representation for human pose estimation
leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]
Smart home (3) competitive product analysis of Intelligent Interaction
Four Byron poems translated from heartless sword
Rk3308-- firmware compilation
Meetup review how Devops & mlops solve the machine learning dilemma in enterprises?
51 timer initial value calculation
Analysis of different dimensions of enterprise reviewers: enterprise growth of Hunan Great Wall Science and Technology Information Co., Ltd
Rk3308--8 channels changed to dual channels + recording gain
When visual studio 2019 is installed, vs installer cannot download files. The progress bar is 0. It shows the solutions to network problems
The fragrant locust flowers on the fragrant locust road have been in a hurry for four years.
Report on development status and investment planning recommendations of global and Chinese fuel filler pipe markets (2022-2028)
Chinese translation of Rilke's autumn with heartless sword
Check whether there is a QR code in the picture
Rk3308 key and LED light
Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
Devops-2- from the Phoenix Project
Application of stop in screen program_ SAP LIUMENG_
Basic use cases for jedis
Live broadcast preview | deconstruct OLAP! The new multidimensional analysis architecture paradigm is fully open! Apache Doris will bring five big issues!