SpringBoot实现单文件上传

网友投稿 429 2023-07-08

SpringBoot实现单文件上传

SpringBoot实现单文件上传

SpringBoot实现单文件上传功能,供大家参考,具体内容如下

架构为springboot+thymeleaf,采用ajax方式提交

1. 页面testFile.html

&lthttp://;title>测试文件上传

单一文件上传实例

文件1:


2. FileController.java

package com.stormkai.controller;

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

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.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

import lombok.extern.slf4j.Slf4j;

@Controller

@RequestMapping("/file")

@Slf4j

public class FileController {

@GetMapping("/index")

public String index() {

return "testFile";

}

@PostMapping("/upload1")

@ResponseBody

public Map upload1(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

log.info("系统路径={}",request.getSession().getServletContext().getRealPath(""));

String path = "F:\\uploadfile\\";

if(!new File(path).exists()){

new File(path).mkdirs();

}

file.transferTo(new File(path + file.getOriginalFilename()));

Map result = new HashMap<>();

result.put("status", "true");

result.put("data", null);

return result;

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot如何设置静态资源缓存一年
下一篇:eclipse启动出现“failed to load the jni shared library”问题解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~