首页 > 编程语言 > 详细

SpringBoot+jsp文件上传

时间:2020-01-06 16:40:18      阅读:85      评论:0      收藏:0      [点我收藏+]
文件上传类
package com.bh.bysj.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

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

/**
* @Description
* @Author sgl
* @Date 2018-05-15 14:04
*/
//文件上传的Controller
@Controller
public class UploadController {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

@GetMapping("/upload")
public String upload() {
return "text";
}

@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}

String fileName = file.getOriginalFilename();
String filePath = "C:\\Users\\我是大帅逼\\Desktop\\bysj\\src\\main\\resources\\static\\";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
LOGGER.info("上传成功");
return "上传成功";
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
return "上传失败!";
}
}
华丽的分割线-------------------------------------------------------------------------------------------------------------------------------------------------------------
application.properties
里面这个复制就奥里给注释里面都有

#访问相关配置
server.port=8086
server.tomcat.uri-encoding=UTF-8
#项目访问名称,如果不配置直接访问bean就可以
#server.servlet.context-path=/demo1
#文件上传的配置
spring.servlet.multipart.max-file-size=100MB
#数据库-->也可以是数据源的连接配置本人用的是alibaba
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/wash
spring.datasource.username=root
spring.datasource.password=dashuaibi
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
#spring.datasource.maxWait=60000
## 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
#spring.datasource.timeBetweenEvictionRunsMillis=60000
## 配置一个连接在池中最小生存的时间,单位是毫秒
#spring.datasource.minEvictableIdleTimeMillis=300000
# 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用
spring.datasource.validationQuery=SELECT ‘x‘
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
spring.datasource.useGlobalDataSourceStat=true

#中间这一大段https://blog.csdn.net/zn65786412qq/article/details/81197819

#Spring boot视图配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
#静态文件访问配置
#spring.mvc.static-path-pattern=/static/**
#spring.resources.static-locations=/static/
#spring.resources.static-locations=classpath:/static/,classpath:/view/,classpath:/public,classpath:/resources,classpath:/META-INF/resources
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.hkrt.demo.bean


spring.mvc.static-path-pattern=/static/**

#
# pagehelper properties
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=false
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

#缓存
#配置缓存名称
#spring.cache.cache-names=c1,c2
#缓存有效期
#spring.cache.redis.time-to-live=1800s
#Redis配置
#spring.redis.database=0
#spring.redis.host=192.168.66.129
#spring.redis.port=6379

## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
#spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
#spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
#spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=1200

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
这波就是前端的jsp注意自己写不复制的一定要写
enctype="multipart/form-data"
不然!!!哼哼哼就会报一个非常狗逼的异常报500
<%--
Created by IntelliJ IDEA.
User: 王泊涵
Date: 2020/1/6
Time: 13:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>
----------------------------------------------------------------
点个赞评个论吧谢谢了



SpringBoot+jsp文件上传

原文:https://www.cnblogs.com/Han-God/p/12156720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!