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)
**项目运行界面如下:**
![](https://img2018.cnblogs.com/blog/1306719/201902/1306719-20190217180013870-1168345695.png)
**访问图片如下:**
![](https://img2018.cnblogs.com/blog/1306719/201902/1306719-20190217180817666-727480628.png)
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的简单实用请参照博主小道仙的后 ...
随机推荐
- Jenkins automate workflow
Now we will build an automate flow from code compiling to product delivery.The essential tools using ...
- leetcode解题报告(22):Two Sum II - Input array is sorted
描述 Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- qsing
qsing1 1.低仿机器人 一道大模拟 2.放爆竹 小辉原本想让小明告诉他,如果同时点燃n串雷,最多会有多长的时间至少有两串雷爆炸的声音是一样的. 但是小辉觉得这个问题真是太简单了,所以决定问小明, ...
- Ansible管理上千台主机时需要的速度优化
1 开启ssh长连接 OpenSSH 5.6版本后SSH支持了Multiplexing 设置参数 ssh_args = -C -o ControlMaster=auto -o ControlPersi ...
- [bzoj 4887] [Tjoi2017]可乐
传送门 Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市 ...
- (转)服务端监控工具:nmon的使用
在性能测试过程中,对服务端的各项资源使用情况进行监控是很重要的一环.这篇博客,介绍下服务端监控工具:nmon的使用方法.. 一.认识nmon 1.简介 nmon是一种在AIX与各种Linux操作系统上 ...
- Vue路由管理之Vue-router
一.Vue Router介绍 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的. ...
- TynSerial流的序列(还原)
TynSerial流的序列(还原) procedure TForm1.ToolButton18Click(Sender: TObject); var serial: TynSerial; ms, ms ...
- 相关 Excel 开源类库性能对比
性能数据 · Excelize 中文文档https://xuri.me/excelize/zh-hans/performance.html Golang library for reading and ...
- PHP 美化输出数组
var_export — 输出或返回一个变量的字符串表示 此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码. 您可以通过将函数的 ...