当前位置:网站首页>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);
}
}
边栏推荐
- 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"
- 隐私计算一体机的应用落地指南——《隐私计算一体机金融应用技术要求》正式发布,助力金融行业数据有序共享
- Devops-3 cloud computing and cloud era operation and maintenance
- Jerry's ble OTA upgrade requires shutting down unnecessary peripherals [chapter]
- Jerry's ble timer clock source cannot choose OSC crystal oscillator [chapter]
- Jerry's ble PB2 cannot be hardware grounded or connected to triode base [chapter]
- Apache atlas quick start
- This paper introduces three feature selection methods in machine learning
- Sm59 remote connection. If you are prompted that there is no host, add host to the server and restart SAP_ SAP LIUMENG
- Jerry's interface for obtaining ble broadcast package and profile data [chapter]
猜你喜欢

卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程,进互联大厂如此简单
![Jerry's ble IO port interrupt and flip [chapter]](/img/e6/bd5bb99ba79ca8daaee844689cd57f.png)
Jerry's ble IO port interrupt and flip [chapter]

2D human posture estimation for posture estimation - associated embedding: end to end learning for joint detection and grouping

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

Li Ling: in six years, how did I go from open source Xiaobai to Apache top project PMC

Add Anaconda's bin directory to path

【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生

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

Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (16)

RK3308--固件编译
随机推荐
【第六节 函数】
[quick code] define the new speed of intelligent transportation with low code
Android 13 re upgrade for intent filters security
Chinese translation of Rilke's autumn with heartless sword
Aggregate sum of MapReduce cases
【历史上的今天】6 月 10 日:Apple II 问世;微软收购 GECAD;发明“软件工程”一词的科技先驱出生
Investment prospect and development strategy planning of China's waste power generation equipment industry 2022-2028 Edition
2D human pose estimation for pose estimation - simdr: is 2D Heatmap representation even necessity for human pose estimation?
Jerry's long press reset and high level reset [chapter]
Four Byron poems translated from heartless sword
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
Link multiple alamofire requests - chain multiple alamofire requests
Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?
Why do I need a thread pool? What is pooling technology?
服务器运维环境安全体系(下篇)
2D human posture estimation for posture estimation - numerical coordinate progression with revolutionary neural networks (dsnt)
Meetup review how Devops & mlops solve the machine learning dilemma in enterprises?
Nanomq newsletter 2022-05 | release of V0.8.0, new webhook extension interface and connection authentication API
Missing setjarbyclass() when running MapReduce task. No class found
Detailed explanation of RGB color space, hue, saturation, brightness and HSV color space