SpringBoot实现单文件上传
作者:KevinYang-凯 发布时间:2023-10-01 21:43:42
标签:SpringBoot,上传
SpringBoot实现单文件上传功能,供大家参考,具体内容如下
架构为springboot+thymeleaf,采用ajax方式提交
1. 页面testFile.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试文件上传</title>
<script src="../static/jquery/jquery-2.1.1.min.js" th:src="@{/jquery/jquery-2.1.1.min.js}"></script>
<script type="text/javascript">
$(function () {
$("#upload1").click(function () {
var formData = new FormData();
formData.append("file", document.getElementById("file").files[0]);
$.ajax({
url: "/file/upload1",
type: "POST",
data: formData,
//必须false才会自动加上正确的Content-Type
contentType: false,
//必须false才会避开jquery对 formdata 的默认处理
//XMLHttpRequest会对 formdata 进行正确的处理
processData: false,
success: function (data) {
if (data.status == "true") {
alert("上传成功!");
}
if (data.status == "error") {
alert(data.msg);
}
},
error: function () {
alert("上传失败!");
}
});
});
});
</script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="/file/upload1">
<fieldset>
<legend>单一文件上传实例:</legend>
文件1:<input type="file" name="file" id="file"/><br/>
<input type="button" id="upload1" value="上传"/><br/>
</fieldset>
</form>
</body>
</html>
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<String, Object> 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<String, Object> result = new HashMap<>();
result.put("status", "true");
result.put("data", null);
return result;
}
}
来源:https://blog.csdn.net/stormkai/article/details/89450646


猜你喜欢
- 在使用C#进行相关编程的时候,有时候我们需要获取系统相关的进程信息。那么在C#中如何获取系统的所有进程那?下面请跟小编一起来操作。1、首先新
- 这篇文章主要介绍了spring cloud Ribbon用法及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- MainActivity如下:package cc.ab;import android.os.Bundle;import android.p
- * 什么是 * Spring MVC中的 * (Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户
- 背景:重做系统后重新配置Android studio 安装虚拟机后无法启动log中显示为启动AVD的进程被杀控制台显示为:在虚拟机列表里没有
- 一、项目简述功能包括: 前台实现:用户浏览菜单、菜品分类筛选、查看菜单详 情、添加购物车、购物车结算、会员券、个人订单查询等 等。 后台实现
- preHandle: 预先处理,在目标的controller方法执行之前,进行处理postHandle: 在目标的con
- 本文实例为大家分享了spring aop注解配置的具体代码,供大家参考,具体内容如下Demo.javapackage cn.itcast.e
- 一个真实的故事大学的时候就开过一门课程,讲设计模式,可是大学生没什么编程实践经验,在大学里面听设计模式的感觉,就像听天书。听着都有道理,可是
- Java的最基本的同步方式,即使用synchronized关键字来控制一个方法的并发访问。 每一个用synchronized关键字声明的方法
- C语言 MD5源码md5c.h:/* POINTER defines a generic pointer type */ typedef u
- Kotlin this详解及实例为了表示当前函数的接收者(receiver), 们使用this表达式:在类的成员函数中,this指向这个类的
- 本文实例为大家分享了Android调用手机摄像头拍照和录音功能的具体代码,供大家参考,具体内容如下调用摄像头拍照:public class
- 一、背景项目中要解析xml,由于Dom4j的诸多优点,我就用Dom4j解析xml,代码如下:public void readXML() {
- 自定义View一直是横在Android开发者面前的一道坎。一、View和ViewGroup的关系从View和ViewGroup的关系来看,V
- 实现的效果图:自定义Fragment继承BottomSheetDialogFragment重写它的三个方法:onCreateDialog()
- 在日常生活中,我们使用maven下载需要的jar包,但是很多的时候由于中央仓库没有,所以我们没有办法下载到需要的jar包,手动去下载上,然后
- 有兴趣的朋友可以回顾一下前两篇java并发编程专题(一)----线程基础知识java并发编程专题(二)----如何创建并运行java线程在现
- 了解JVM内存结构的目的在Java的开发过程中,因为有JVM自动内存管理机制,不再需要像在C、C++开发那样手动释放对象的内存空间,不容易出
- HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决