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. luogu P3567 [POI2014]KUR-Couriers

    二次联通门 : luogu P3567 [POI2014]KUR-Couriers MMP 指针 RE + MLE + WA..... 不得已...向黑恶的数组实力低头 /* 指针 */ #inclu ...

  2. javaee三层架构案例--简单学生管理系统

    背景 学了jdbc.jsp等需要串起来,不然会忘记 项目环境 win10 jdk11 mysql8.0.13 jar包 c3p0-0.9.5.2 commons-dbutils-1.7 jstl mc ...

  3. IO流——常用IO流详解

    1:字节流 字节流:用于处理以字节为单位的二进制文件(如音乐,图片等) InputStream 是抽象类 它的对应子类FileInputStream可以被实例化 构造方法: FileInputStre ...

  4. Javescript——数据类型

    原文链接:Understanding Data Types in JavaScript Data types are used to classify one particular type of d ...

  5. QML显示圆形图片

    Item {//一个圆形图片 width: parent.width height: parent.height Image { id: rdJpg anchors.centerIn: parent ...

  6. 123456---com.twoapp.huanYingMotro--- 幻影摩托

    123456---com.twoapp.huanYingMotro--- 幻影摩托

  7. InfluxDB+Grafana大数据监控系列之数据源配置(二)

    一.Grafana 配置 InfluxDB 数据源 1.1 登录 Granfana 界面选择 InfluxDB 数据源 在前面我们已经部署好相应监控环境,登录Grafana:http://10.223 ...

  8. 【opencv】split

    opencv-split Divides a multi-channel array into several single-channel arrays. code ,,,,,,,,,,,}; Ma ...

  9. ES6-类(Class)

    ES6躬行记(20)——类 ES6正式将类(Class)的概念在语法层面标准化,今后不必再用构造函数模拟类的行为.而ES6引入的类本质上只是个语法糖(即代码更为简洁.语义更为清晰),其大部分功能(例如 ...

  10. CentOS 7 vi详解

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...