当前位置:网站首页>外卖系统 文件上传
外卖系统 文件上传
2022-07-28 04:37:00 【为什么不好好卖蛋饼】
04 文件上传
/** * 文件上传和下载 */
@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
@Value("${reggie.path}")
private String basePath;
@PostMapping("/upload")
public String upload(MultipartFile file){
log.info(file.toString());
//原始文件名
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//使用uuid重新生成文件名
String fileName = UUID.randomUUID().toString() + suffix;
//创建目录对象
File dir = new File(basePath);
//判断目录是否存在
if(!dir.exists()){
//目录不存在,需要创建
dir.mkdirs();
}
try{
//将临时文件转存到指定位置
file.transferTo(new File(basePath+"hello.jpg"));
}catch (IOException ex){
ex.printStackTrace();
}
return fileName;
}
/** * 文件下载 */
@GetMapping("/download")
public void download(String name, HttpServletResponse response){
try{
//输入流
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
//输出流
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
int len=0;
byte[] bytes=new byte[1024];
while ((len=fileInputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
//关闭资源
outputStream.close();
fileInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
边栏推荐
- mysql分区表改造
- MySQL数据库————初识数据库
- Advanced architects, 16 common principles of microservice design and Governance
- 【sylar】框架篇-Chapter23-模块篇总结
- High number_ Chapter 4__ Curvilinear integral_ Exercise solution
- Select sorting method
- Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation
- Study of the Intel aria 10 Avalon mm DMA interface for PCI Express solutions User Guide
- Some personal understandings of openpose
- pytorch打包exe出现WARNING: file already exists but should not: C:\Users\workAI\AppData\Local\Temp\_MEI13
猜你喜欢

重要的 SQL Server 函数 - 其他函数

重要的 SQL Server 函数 - 数字函数

Information system project manager (2022) - key content: Project Procurement Management (12)

Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers
![[blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]](/img/72/10bd8a292c3ee3445907795ebf2ae8.png)
[blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]

重要的 SQL Server 函数 - 日期函数

【Oracle】083错题集

Web渗透之域名(子域名)收集方法

Pyqt based grouping tool

Campus stray cat information recording and sharing applet source code
随机推荐
Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers
【sylar】框架篇-Chapter24-支持业务模块化
[Sylar] framework chapter -chapter10-address module
Object locking in relational database transactions
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]
pytorch_ Lightning in lightning_ The output of hparams.yaml in logs is null
Solana's "deceptive behavior": making mobile phones and opening stores
What is the account opening process of qiniu business school? Is it safe?
MySQL partition table transformation
[Sylar] framework -chapter14 tcpserver module
21 openwrt kernel module changed to.Ko automatic loading
[Sylar] framework -chapter7-io coordination scheduling module
Important SQL server functions - string utilities
Attempt method in laravel user authentication
[blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]
C语言初阶——循环语句(while,for,do while)
Information system project manager (2022) - key content: organization level project management, process management, project set management (18)
Gerrit operation - rollback a patch_ set
Bio annotation of emotion analysis aste triples extraction
How much does it cost to build a self built server for ark survival evolution?