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 配置虚拟路径的更多相关文章

  1. Tomcat配置虚拟路径访问容器外的硬盘资源

    问题: 如果tomcat中上传了很多的图片,会导致tomcat启动的时候会慢,所以应该把图片上传到tomcat容器外部 那么,问题来了: tomcat出于安全考虑,禁止了直接访问外部硬盘资源. 解决: ...

  2. myeclipse中tomcat配置虚拟路径,用于存储及上传图片

    由于双击tomcat后只出现了overview窗口,没有出现Models窗口,如下图所示,故eclipse中的方法不能用. 采用另一种方法 在自己的tomcat的安装目录下 ( 我安装目录的是 D:\ ...

  3. tomcat配置虚拟路径保存、访问图片

    一些项目中往往需要上传一些图片文件之类,一般不建议直接保存在数据库内,往往是讲图片等资源保存在服务器的某个文件夹下,传统做法是上传到部署目录下,通过相对路径进行访问.这样当我们系统需要进行升级,进行全 ...

  4. 关于Tomcat配置虚拟路径保存、访问图片

    在项目中往往需要上传一些图片文件之类,一般不建议直接保存在数据库内,往往是讲图片等资源保存在服务器的某个文件夹下,传统做法是上传到部署目录下,通过相对路径进行访问. 这样当我们系统需要进行升级,进行全 ...

  5. Tomcat 配置虚拟路径保存、访问图片

    转载自:https://www.cnblogs.com/magic101/p/7756402.html 配置tomcat的虚拟映射路径 1.修改Tomcat的server.xml文件 <Host ...

  6. springboot内置tomcat配置虚拟路径

    在Springboot中默认的静态资源路径有:classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classp ...

  7. tomcat 配置虚拟路径

    把图片或者其他的文件传到webapps以外的目录 <Context docBase= "e:\image\"  path= "/uploads"  rel ...

  8. eclipse配置虚拟路径后,每次启动tomcat都会虚拟路径失效的问题解决

    由于,eclipse启动tomcat部署项目并不是直接把项目放到tomcat的webapps目录下的,而是从我们在eclipse配置的外部tomcat中取出二进制文件,在eclipse内部插件中作为t ...

  9. Tomcat映射虚拟路径到指定磁盘(eclipse)

    用WangEditor富文本编辑,上传图片的时候,本文主要记录一下Tomcat映射虚拟路径到指定磁盘,保存到指定路径中,且能实现页面预览. 在实现之前wangeditor的简单实用请参照博主小道仙的后 ...

随机推荐

  1. bzoj 5206

    $n$ 点 $m$ 边图的有限制三元环个数 首先将所有左右端点并且属性相同的边的权值相加,合并为一条边 在这只之前得先排序排序之前得先判断是否需要交换左右端点的位置 T_T 然后统计三元环 补充说明按 ...

  2. linux系列(二十):find命令

    1.命令格式 find pathname -options [-print -exec -ok ...] 2.命令功能 用于在文件树种查找文件,并作出相应的处理 3.命令参数 pathname: fi ...

  3. qml 绘制高精地图之怀疑人生的加载速度

    绘制高精地图时需要gps的经纬度坐标,之前的实现方式是QGeocoordinate类的经纬度变量通过json的方式在qml中使用. 以画线为例,使用方式是这样哒. for(var i in vehic ...

  4. 【原创】go语言学习(十三)struct介绍2

    目录: 方法的定义 函数和方法的区别 值类型和指针类型 面向对象和继承 结构体和json序列化 方法的定义 1.和其他语言不一样,Go的方法采⽤用另外一种方式实现. package main impo ...

  5. Allure自动化测试报告之修改allure测试报告logo

    1.安装allure 2.进入 /usr/local/Cellar/allure/2.10.0/libexec/config 3.在allure.yml添加 - custom-logo-plugin ...

  6. selenium鼠标下滑操作

    # coding = utf-8 import time from selenium import webdriver from selenium.webdriver.common.by import ...

  7. [TJOI2019]甲苯先生的滚榜——非旋转treap

    题目链接: [TJOI2019]甲苯先生的滚榜 要求维护一个二维权值的集合并支持单点修改,用平衡树维护即可. 因为$n\le 10^6$但$m\le 10^5$,所以最多只有$10^5$个人被操作. ...

  8. Java-JUC(零):Java:现有线程T1/T2/T3,如何确保T1执行完成之后执行T2,T3在T2执行完成之后执行。

    要实现多个线程执行完成先后,就要知道如何实现线程之间的等待,java线程等待实现是join.java的jdk中join方法实现如下: public final synchronized void jo ...

  9. C#Winform ListView中没有Item双击事件的两种实现方法!

    第一种: //if (this.listView1.FocusedItem != null) //{ // if (this.listView1.SelectedItems != null) // { ...

  10. vue-cli及vue-router

    1.单文件组件 (1)问题:    使用 Vue.component 来定义全局组件,紧接着用 new Vue({ el: ‘#container ‘}) 在每个页面内指定一个容器元素.当在更复杂的项 ...