前端动态获取servlet虚拟路径_layui实现图片虚拟路径上传,预览和删除的例子

效果如下所示:

前端:

#detailTbody tr:hover {

background: #fff;

}

.layui-form-label {

width: 110px;

}

.uploader-list {

margin-left: -15px;

}

.uploader-list .info {

position: relative;

margin-top: -25px;

background-color: black;

color: white;

filter: alpha(Opacity=80);

-moz-opacity: 0.5;

opacity: 0.5;

width: 100px;

height: 25px;

text-align: center;

display: none;

}

.uploader-list .handle {

position: relative;

background-color: #ff6a00;

color: white;

filter: alpha(Opacity=80);

-moz-opacity: 0.5;

width: 100px;

text-align: right;

height: 18px;

margin-bottom: -18px;

display: none;

}

.uploader-list .handle span {

margin-right: 5px;

}

.uploader-list .handle span:hover {

cursor: pointer;

}

.uploader-list .file-iteme {

margin: 12px 0 0 15px;

padding: 1px;

float: left;

}

单据上传(可上传多张)

预览图:

单据

js:

layui.use(['form', 'layer', 'laydate', 'upload'], function () {

$ = layui.jquery;

var form = layui.form,

layer = layui.layer,

laydate = layui.laydate,

upload = layui.upload;

//多图片上传

upload.render({

elem: '#test2'

, url: '/psi/order/uploadImg'

, multiple: true

, before: function (obj) {

layer.msg('图片上传中...', {

icon: 16,

shade: 0.01,

time: 0

})

}

, done: function (res) {

layer.close(layer.msg());//关闭上传提示窗口

//上传完毕

$('#uploader-list').append(

'

' +

'

' +

'' +

'

'

);

}

});

});

$(document).on("mouseenter mouseleave", ".file-iteme", function (event) {

if (event.type === "mouseenter") {

//鼠标悬浮

$(this).children(".info").fadeIn("fast");

$(this).children(".handle").fadeIn("fast");

} else if (event.type === "mouseleave") {

//鼠标离开

$(this).children(".info").hide();

$(this).children(".handle").hide();

}

});

$(document).on("click", ".file-iteme .handle", function(event){

$(this).parent().remove();

})

})

function showBig(obj) {

var url = (obj.src);

var index = layer.open({

type: 2,

content: url,

area: ['100%', '100%'],

title: "单据",

maxmin: true,

closeBtn: 1

});

layer.full(index);

}

controller层

@RequestMapping(value = "/uploadImg")

@ResponseBody

public Map uploadImg(MultipartFile file,HttpServletRequest request){

Map data = new HashMap<>();

String url = "";

if (!file.isEmpty()){

url = FileUploadUtil.saveImage(file,"orderVoucher",request);

}

data.put("url",url);

return data;

}

FileUploadUtil类

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.*;

import java.util.Date;

public class FileUploadUtil {

public static String fileUploadPathUrl="D:\\svnproject\\wechatprintingPicture";

/**

* 图片读取存放获取路径

*

* @param file 文件

* @param fileName 文件存放的目录名

* @return

*/

public static String saveImage(MultipartFile file, String fileName, HttpServletRequest requestFileUploadUtil) {

long timestamp = new Date().getTime();//获取时间戳

String realPath = fileUploadPathUrl;//项目路径

String newFileName = timestamp + "" + file.getOriginalFilename(); //file.getOriginalFilename()是获取原始图片的拓展名,newfileName新的文件名字

String path = realPath + "/" + fileName;

String newPath = path + "/" + newFileName;图片存放的位置路径

File filePath = new File(path + "/");

if (!filePath.exists()) {

filePath.mkdirs();

}

if (!file.isEmpty()) {

BufferedOutputStream out = null;

try {

out = new BufferedOutputStream(

new FileOutputStream(new File(newPath)));

out.write(file.getBytes());

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

String url = requestFileUploadUtil.getScheme() + "://" + requestFileUploadUtil.getServerName() + ":" + requestFileUploadUtil.getServerPort() + requestFileUploadUtil.getContextPath() + "/" + fileName + "/" + newFileName;

return url;

}

}

yml虚拟路径配置

spring:

resources:

static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.uploadPath}

web:

uploadPath: D:/svnproject/wechatprintingPicture

以上这篇layui实现图片虚拟路径上传,预览和删除的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

本文标题: layui实现图片虚拟路径上传,预览和删除的例子

本文地址: http://www.cppcns.com/wangluo/javascript/276201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JSP和Servlet中,可以通过设置虚拟路径来引用上传文件的图片。以下是一个示例代码,演示如何设置虚拟路径: 1. 在Servlet中,获取应用程序的实际路径,并将其设置为ServletContext的属性: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取应用程序的实际路径 String applicationPath = request.getServletContext().getRealPath(""); // 设置虚拟路径 String virtualPath = "/uploads"; // 虚拟路径,可以根据需要修改 // 将虚拟路径保存到ServletContext的属性中 request.getServletContext().setAttribute("virtualPath", virtualPath); // 其他文件上传处理逻辑... } ``` 2. 在JSP中,使用EL表达式获取ServletContext的属性,并将虚拟路径上传文件名拼接成完整的URL: ```html <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <img src="${pageContext.servletContext.getAttribute('virtualPath')}/${fileName}"> ``` 在上述代码中,`${pageContext.servletContext.getAttribute('virtualPath')}`通过EL表达式获取ServletContext的属性值,`${fileName}`为上传的文件名。将两者拼接在一起,就可以得到完整的图片URL。 请注意,在设置虚拟路径时,需要确保虚拟路径与实际存储文件的目录相对应。在示例中,虚拟路径为`/uploads`,则上传的文件应该存储在实际路径下的`uploads`目录中。你可以根据实际情况修改虚拟路径和存储目录的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值