Layui 上传图片到磁盘上 + Tomcat 配置虚拟路径
Layui 上传图片到磁盘上 + Tomcat 配置虚拟路径
Tomcat 配置虚拟路径
找到 eclipse 中 tomcat 下面的 server.xml
文件,在 Host
标签里面添加 <Context docBase="E:\upload\image\" path="/upload/image" reloadable="true"/>
。
其中,docBase 表示的是图片的实际路径, path 表示的是图片的虚拟路径。
在项目根目录下面建立图片的访问路径,即 path 指向的是图片的虚拟路径,如下所示:
图片在磁盘上的实际保存地址如下:
数据库中存放图片的访问路径:
Layui 上传图片到磁盘上
index.jsp 文件
${webTitle}
大家好,我来复习springmvc了
学号 | 姓名 | 年龄 | 性别 | 班级 |
---|---|---|---|---|
${student.sno } | ${student.sname } | ${student.age } | ${student.gender } | ${student.grade } |
常规使用:普通图片上传
上传图片
```
StudentInfoSelect.java 文件
package com.libin.springmvc.controller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.libin.springmvc.entity.People;
import com.libin.springmvc.entity.Student;
import com.libin.springmvc.pub.PubConfig;
import com.libin.springmvc.service.PeopleService;
import com.libin.springmvc.service.StudentService;
import com.libin.springmvc.utils.WebUtil;
@RequestMapping("/student")
@Controller
public class StudentInfoSelect {
@Resource
private StudentService studentService;
@Resource
private PeopleService peopleService;
@Autowired
private PubConfig pubConfig;
@RequestMapping("/info")
public String studentInfo(Model model, Student student) {
List<Student> list = studentService.findAllStudent();
People peopleName = new People();
peopleName.setName("Tom");
People people = peopleService.getPeopleByName(peopleName);
model.addAttribute("list", list);
model.addAttribute("people", people);
return "index";
}
@RequestMapping("/imageUpload")
public void imageUpload(HttpServletRequest request, HttpServletResponse response, MultipartFile file) {
Map<String, Object> dataMap = new HashMap<>();
if (!file.isEmpty()) {
try {
// String contextPath = PropertiesUtil.getValue("imageUploadPath") + "\\image";
String contextPath = pubConfig.getImageUploadPath() + "\\image";
String img = uploadFile(file, contextPath);
String imgPath = "upload/image/" + img;
dataMap.put("code", 0);
dataMap.put("image", imgPath);
People people = new People();
people.setName("Tom");
people.setImagepath(imgPath);
int row = peopleService.updatePeopleImage(people);
dataMap.put("row", row);
} catch (Exception e) {
dataMap.put("code", 1);
e.printStackTrace();
}
}
WebUtil.responseOutWithJson(response, dataMap);
}
public static String uploadFile(MultipartFile file, String filePath) throws IllegalStateException, IOException {
Random r = new Random();
String name = file.getOriginalFilename(); // 文件的真实名称
String fileName = getShortSystemTime() + r.nextInt(99999999) + "_" + name;
File tempFile = new File(filePath, fileName);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
if (tempFile.exists()) {
tempFile.delete();
}
tempFile.createNewFile();
file.transferTo(tempFile);
return tempFile.getName();
}
public static String getShortSystemTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
return df.format(new Date()).toString();
}
}
</div>
## 显示界面
<b style="color:red;">完整项目地址:</b>[testspringmvc](https://github.com/hglibin/shiyanlou/tree/master/testspringmvc)
**项目运行界面如下:**

**访问图片如下:**

Layui 上传图片到磁盘上 + Tomcat 配置虚拟路径的更多相关文章
- Tomcat配置虚拟路径访问容器外的硬盘资源
问题: 如果tomcat中上传了很多的图片,会导致tomcat启动的时候会慢,所以应该把图片上传到tomcat容器外部 那么,问题来了: tomcat出于安全考虑,禁止了直接访问外部硬盘资源. 解决: ...
- myeclipse中tomcat配置虚拟路径,用于存储及上传图片
由于双击tomcat后只出现了overview窗口,没有出现Models窗口,如下图所示,故eclipse中的方法不能用. 采用另一种方法 在自己的tomcat的安装目录下 ( 我安装目录的是 D:\ ...
- tomcat配置虚拟路径保存、访问图片
一些项目中往往需要上传一些图片文件之类,一般不建议直接保存在数据库内,往往是讲图片等资源保存在服务器的某个文件夹下,传统做法是上传到部署目录下,通过相对路径进行访问.这样当我们系统需要进行升级,进行全 ...
- 关于Tomcat配置虚拟路径保存、访问图片
在项目中往往需要上传一些图片文件之类,一般不建议直接保存在数据库内,往往是讲图片等资源保存在服务器的某个文件夹下,传统做法是上传到部署目录下,通过相对路径进行访问. 这样当我们系统需要进行升级,进行全 ...
- Tomcat 配置虚拟路径保存、访问图片
转载自:https://www.cnblogs.com/magic101/p/7756402.html 配置tomcat的虚拟映射路径 1.修改Tomcat的server.xml文件 <Host ...
- springboot内置tomcat配置虚拟路径
在Springboot中默认的静态资源路径有:classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classp ...
- tomcat 配置虚拟路径
把图片或者其他的文件传到webapps以外的目录 <Context docBase= "e:\image\" path= "/uploads" rel ...
- eclipse配置虚拟路径后,每次启动tomcat都会虚拟路径失效的问题解决
由于,eclipse启动tomcat部署项目并不是直接把项目放到tomcat的webapps目录下的,而是从我们在eclipse配置的外部tomcat中取出二进制文件,在eclipse内部插件中作为t ...
- Tomcat映射虚拟路径到指定磁盘(eclipse)
用WangEditor富文本编辑,上传图片的时候,本文主要记录一下Tomcat映射虚拟路径到指定磁盘,保存到指定路径中,且能实现页面预览. 在实现之前wangeditor的简单实用请参照博主小道仙的后 ...
随机推荐
- NetworkX系列教程(10)-算法之五:广度优先与深度优先
小书匠Graph图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把图 ...
- OpenCV:Python下OpenCV安装和入门最强详细攻略
一.关于OpenCV简介 OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效— ...
- centos7mongo集群
1.安装 cat > /etc/yum.repos.d/mongodb.repo << EOF[mongodb-org-3.6]name=MongoDB Repositorybase ...
- jquery入门使用
目录 jquery入门使用 1.jQuery简介 jQuery介绍 jQuery优点 2.jQuery的load方法 load方法介绍 load实例 代码 3.jQuery的get方法 get()方法 ...
- 【SPOJ】Longest Common Substring
[SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...
- BZOJ2716天使玩偶
不会KD-tree怎么办?CQD硬搞. 建立正常的平面直角坐标系,首先我们只考虑在目标点左下角的点对目标点的贡献,由于左下点的横纵坐标都小于目标点,那么曼哈顿距离就可以化简了,绝对值去掉后,得到$x2 ...
- Java核心复习——J.U.C AbstractQueuedSynchronizer
第一眼看到AbstractQueuedSynchronizer,通常都会有这几个问题. AbstractQueuedSynchronizer为什么要搞这么一个类? 这个类是干什么的.有什么用? 这个类 ...
- [Ubuntu] A start job is running for...interfaces
CPU:RK3288 系统:Linux 移植 Ubuntu 16.04 到嵌入式平台,如果以太网有问题,在这里会耗时大约5分钟 开机后可以修改 Ubuntu 配置来缩短时间 打开下面的文件,可以看到最 ...
- OpenJudge计算概论-年龄与疾病
/*========================================================== 年龄与疾病 总时间限制: 1000ms 内存限制: 65536kB 描述 某医 ...
- HTTP和WSGI协议
HTTP协议简介 超文本传输协议(HyperText Transfer Protocol)是一种应用层协议.HTTP是万维网的数据通信的基础.设计HTTP最初的目的是为了提供一种发布和接收HTML页面 ...