当前位置:网站首页>Simple file upload
Simple file upload
2022-06-10 16:36:00 【Java- please give me more advice】
SpirigBoot project
rely on
<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>The main class :
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);
}
}
Result set class :
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"," Upload successful ",body);
}
public static<E> Resp<E> fail(String code,String message){
return new Resp(code,message,(Object)null);
}
}
Interface
import com.example.uploadtest.entity.Resp;
import org.springframework.web.multipart.MultipartFile;
public interface UploadService {
public Resp<String> upload(MultipartFile file);
}
Interface implementation class
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"," The file is empty !");
// Get the original name of the file
String originalFilename = file.getOriginalFilename();
// Get a new file name based on the number of milliseconds
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+" Upload failed !");
}
return Resp.success(fileName);
}
}
Controllerc Control layer
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);
}
}
边栏推荐
- 软件测试架构师,给后辈的16条忠告,快看看别错过
- 2022 underground coal mine electrical examination question bank and online simulation examination
- Conversion between localdate and date
- Query convert quickview is a grayed out solution (turn)_ SAP LIUMENG
- leetcode:730. Statistics of different palindrome subsequences [traversed by point and surface interval DP + 3D DP + diagonal]
- [section 6 functions]
- 【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生
- Duyuan outdoor sprint to Shenzhen Stock Exchange: the annual revenue is 350million, and the color of Lin Xizhen family is obvious
- Global and Chinese intelligent elderly care system industry 2 competition analysis and Investment Strategic Planning Research Report 022-2028
- Aggregate sum of MapReduce cases
猜你喜欢

The command set has reached strategic cooperation with Yingmin technology, and the domestic original Internet of things operating system has helped to make power detection "intelligent"

卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程,進互聯大廠如此簡單

STOP在屏幕程序的应用_SAP刘梦_

Middle office: Data middle office, business middle office, technology middle office, application middle office, AI middle office

迪赛智慧数——文字(文本墙):80后儿童时期风靡的25种玩具

What are the pitfalls of redis's current network: using a cache and paying for disk failures?

Roll up and break through the anxiety of 35 years old. The animation demonstrates how easy it is for the CPU to record the function call process and enter the Internet factory

2D human posture estimation for posture estimation - simple baseline (SBL)
![Jerry's long press reset and high level reset [chapter]](/img/08/900aba6e419cda2f00c3f289ad615e.png)
Jerry's long press reset and high level reset [chapter]

袋鼠云数栈基于CBO在Spark SQL优化上的探索
随机推荐
STOP在屏幕程序的应用_SAP刘梦_
Chinese translation of Rilke's autumn with heartless sword
Join operation cases in the map phase of MapReduce
Troubleshooting of cl210openstack operation -- troubleshooting of common core problems
测试用例常用方法和选择原则
Why__ Inline usually needs static
Effect comparison and code implementation of three time series hybrid modeling methods
Aggregate sum of MapReduce cases
AI video cloud: a good wife in the era of we media
PV operation daily question - black and white chess question (variant)
This paper introduces three feature selection methods in machine learning
袋鼠云数栈基于CBO在Spark SQL优化上的探索
Devops-3 cloud computing and cloud era operation and maintenance
Come with me to understand gaussdb (for opengauss) [Gauss is not a mathematician this time]
[quick code] define the new speed of intelligent transportation with low code
Jerry's ble PB2 cannot be hardware grounded or connected to triode base [chapter]
迪赛智慧数——文字(文本墙):80后儿童时期风靡的25种玩具
Nerf: the popularity of using deep learning to complete 3D rendering tasks
51 timer initial value calculation
Devops-2- from the Phoenix Project