文件上传功能实现

简介: 文件上传功能实现

很多小伙伴会遇到图片上传的功能,下面给大家分享图片上传的具体代码实现。话不多,直接上代码:

package org.example.demo2.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin(origins ="*")
@RestController
@RequestMapping("/param")
@Validated
public class FileParamController{
    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        String uploadFile = "C:\\upload\\images";
        // 1:指定文件上传的目录
        //获取当前年月
        // 获取当前年月
        LocalDate now = LocalDate.now();
        String yearMonth = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
        uploadFile=uploadFile+"\\"+yearMonth;
        File targetFile = new File(uploadFile);
        try {
            // 2:如果targetFile不存在,则创建
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            // 3: 指定文件上传后的目录
            String fileName = file.getOriginalFilename();
            File targetFileName = new File(targetFile, fileName); // 先写死
            // 4:文件上传到指定的目录
            file.transferTo(targetFileName);
            Map map=new HashMap<>();
            map.put("filename",fileName);
            map.put("code",200);
            return "/FinforWorx/images/"+yearMonth+"/"+fileName;
        } catch (IOException e) {
            e.printStackTrace();
            Map map=new HashMap<>();
            map.put("des","上传错误");
            map.put("code",500);
            return "上传错误";
        }
    }
}
相关文章
|
9月前
|
存储
若依框架 --- pdf文件上传预览功能实现
若依框架 --- pdf文件上传预览功能实现
406 0
|
9月前
|
开发框架 移动开发 小程序
uniapp实现上传文件功能
uniapp实现上传文件功能
651 0
|
网络协议
文件上传案例
文件上传案例
|
4月前
|
JSON 前端开发 Go
前端文件下载的方式
【10月更文挑战第5天】
142 58
|
4月前
|
监控 前端开发 安全
C#一分钟浅谈:文件上传与下载功能实现
【10月更文挑战第2天】在Web应用开发中,文件的上传与下载是常见需求。本文从基础入手,详细讲解如何在C#环境下实现文件上传与下载。首先介绍前端表单设计及后端接收保存方法,使用`&lt;input type=&quot;file&quot;&gt;`与`IFormFile`接口;接着探讨错误处理与优化策略,如安全性验证和路径管理;最后讲解文件下载的基本步骤,包括确定文件位置、设置响应头及发送文件流。此外,还提供了进阶技巧,如并发处理、大文件分块上传及进度监控,帮助开发者构建更健壮的应用系统。
219 15
|
4月前
|
前端开发 JavaScript
💥【exceljs】纯前端如何实现Excel导出下载和上传解析?
本文介绍了用于处理Excel文件的库——ExcelJS,相较于SheetJS,ExcelJS支持更高级的样式自定义且易于使用。表格对比显示,ExcelJS在样式设置、内存效率及流式操作方面更具优势。主要适用于Node.js环境,也支持浏览器端使用。文中详细展示了如何利用ExcelJS实现前端的Excel导出下载和上传解析功能,并提供了示例代码。此外,还提供了在线调试的仓库链接和运行命令,方便读者实践。
611 5
|
9月前
|
JSON Rust 前端开发
【sheetjs】纯前端如何实现Excel导出下载和上传解析?
本文介绍了如何使用`sheetjs`的`xlsx`库在前端实现Excel的导出和上传。项目依赖包括Vite、React、SheetJS和Arco-Design。对于导出,从后端获取JSON数据,通过`json_to_sheet`、`book_new`和`writeFile`函数生成并下载Excel文件。对于上传,使用`read`函数将上传的Excel文件解析为JSON并发送至后端。完整代码示例可在GitHub仓库[fullee/sheetjs-demo](https://github.com/fullee/sheetjs-demo)中查看。
493 10
|
9月前
|
移动开发 前端开发
VForm3的文件上传方式
VForm3的文件上传方式
278 0
|
前端开发
前端常规关于网页文件下载的问题
前端常规关于网页文件下载的问题
83 0
|
数据库
对象管理器设计-ConnectionManager 文件上传题 1. 对象管理器设计
对象管理器设计-ConnectionManager 文件上传题 1. 对象管理器设计