栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Vue + SpringBoot + MultipartFile 实现文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vue + SpringBoot + MultipartFile 实现文件上传

前端使用 Vue 官方组件进行文件上传:


	选取文件
	上传到服务器


后端使用的是 Spring MultipartFile 来接收上传之后的文件内容。MultipartFilehttps://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html

 如 MultipartFile 官方文档所说:

The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storage will be cleared at the end of request processing.

文件内容要么存储在内存中,要么暂时存储在磁盘上。在任一情况下,用户都负责根据需要将文件内容复制到会话级或持久性存储。临时存储将在请求处理结束时清除。

 所以这是要求我们将临时文件转存到固定目录:

void transferTo(File dest)
         throws IOException,
                IllegalStateException
Transfer the received file to the given destination file.

default void transferTo(Path dest)
                 throws IOException,
                        IllegalStateException
Transfer the received file to the given destination file.
The default implementation simply copies the file input stream.

所以我的实现是:

package org.democracy.rest.controller;

import com.alibaba.fastjson.JSONObject;
import org.democracy.commom.base.AjaxResult;
import org.democracy.dao.entity.UploadFile;
import org.democracy.service.UploadFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("uploadfile")
public class UploadFileController {
    private static final Logger logger = LoggerFactory.getLogger(UploadFileController.class);

    @Autowired
    private UploadFileService uploadFileService;

    @Value(value = "${app.file.directory}")
    private String fileBaseDirectory;


    @PostMapping("up")
    public AjaxResult uploadFiles(HttpServletRequest request, MultipartFile[] file) throws IOException {
        if (file != null && file.length > 0) {
            for (int i=0;i < file.length; i++) {
                MultipartFile multipartFile = file[i];
                if (!multipartFile.isEmpty()){
                    String fileName = multipartFile.getOriginalFilename();
                    File tmpFile = new File(fileBaseDirectory, fileName);
                    if (!tmpFile.getParentFile().exists()) {
                        tmpFile.getParentFile().mkdir();
                    }
                    multipartFile.transferTo(new File(fileBaseDirectory + File.separator + fileName));
                }
            }
        }
        return AjaxResult.success("123");
    }
}

 至此完成文件上传功能!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号