当前位置:网站首页>简单实现文件上传
简单实现文件上传
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);
}
}
边栏推荐
- Sorting of MapReduce cases
- Jerry's ble IO port interrupt and flip [chapter]
- Software College of Shandong University Project Training - Innovation Training - network security range experimental platform (16)
- Aggregate sum of MapReduce cases
- 服务器运维环境安全体系(下篇)
- RK3308 按键Key与LED灯
- Rk3308--8 channels changed to dual channels + recording gain
- Meetup review how Devops & mlops solve the machine learning dilemma in enterprises?
- Join operation cases in the reduce phase of MapReduce
- PV operation daily question - Restaurant Service
猜你喜欢

Detailed explanation of RGB color space, hue, saturation, brightness and HSV color space

Effect comparison and code implementation of three time series hybrid modeling methods

Global and Chinese intelligent elderly care system industry 2 competition analysis and Investment Strategic Planning Research Report 022-2028

Basic use cases for jedis

PV operation daily question - single log bridge problem

Analysis report on market demand trend and sales strategy of global and Chinese automatic marshmallow machines (2022-2028)

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

STM32 printf garbled

智能家居(3)智能交互的竞品分析

How to realize the website dark mode
随机推荐
Print linked list from end to end (6)
New York financial regulators issue official guidelines for stable currency
PHP wechat H5 payment demo
Common sense: the number of neurons in the brain of mice is about 70million and that of humans is about 86billion
Customization development of defi cross chain mining DAPP system
Fosun Group hekaiduo: grow together with Alibaba cloud and help business innovation
Come with me to understand gaussdb (for opengauss) [Gauss is not a mathematician this time]
Query-Convert QuickView是灰掉的解决办法(转)_SAP刘梦
PV operation daily question - exam questions
[section 6 functions]
[section 14 STL container II]
Matlab learning notes (3) advanced operation of MATLAB matrix
Jerry's ble abnormal power consumption [chapter]
PV operation daily question 1 - single wooden bridge problem (variant 2)
Actual combat of software testing e-commerce project (actual combat video station B has been released)
Research Report on development potential analysis and investment development strategy of global and Chinese hydraulic oil industry (2022-2028)
PV operation daily question - single log bridge problem
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
torch.nn.utils.rnn.pad_sequence()详解【Pytorch入门手册】
Many MapReduce cases seek common friends in the map stage