layui java 图片上传_spring-layui-图片上传预览

前端:

文件上传demo

常规使用:普通图片上传

上传图片

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

var form = layui.form;

var $ = layui.jquery;

var upload = layui.upload;

upload.render({

elem: '#test1'

,url: '/load/upload'

,before: function(obj){

//预读本地文件示例,不支持ie8

obj.preview(function(index, file, result){

$('#demo1').attr('src', result); //图片链接(base64)

});

}

,done: function(res){

//如果上传失败

if(res.code > 0){

return layer.msg('上传失败');

}

//上传成功

}

});

});

后端:

package com.demo;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Calendar;

import java.util.UUID;

import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

@Controller

public class Load {

@RequestMapping(value = "/upload")

@ResponseBody

public String upload( MultipartFile file, HttpServletRequest request) {

//System.out.println("file"+file);

Calendar currTime = Calendar.getInstance();

String time = String.valueOf(currTime.get(Calendar.YEAR))+String.valueOf((currTime.get(Calendar.MONTH)+1));

String path ="d:"+File.separator+"img"+File.separator+time;

String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

suffix = suffix.toLowerCase();

if(suffix.equals(".jpg") || suffix.equals(".jpeg") || suffix.equals(".png") || suffix.equals(".gif")){

String fileName = UUID.randomUUID().toString()+suffix;

File targetFile = new File(path, fileName);

if(!targetFile.getParentFile().exists()){//注意,判断父级路径是否存在

targetFile.getParentFile().mkdirs();

}

long size = 0;

//保存

try {

file.transferTo(targetFile);

size = file.getSize();

} catch (Exception e) {

e.printStackTrace();

}

JSONObject result = new JSONObject();

result.put("fileUrl", "/img/"+time+fileName);

result.put("url", "/img/"+time+fileName);

result.put("state", "SUCCESS");

result.put("title", fileName);

result.put("original", fileName);

result.put("type", suffix);

result.put("size", size);

return result.toString();

}else{

JSONObject result = new JSONObject();

result.put("ss", false);

result.put("msg", "格式不支持");

return result.toString();

}

}

}

spring-mvc-xml:

xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

controller:

package com.demo;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class Testhello{

@RequestMapping("/hello") //对应于index.jsp的hello请求

public String hello(Model model) {

model.addAttribute("message", "我是springmvc");

return "hello"; //返回到view的hello.jsp

// TODO Auto-generated method stub

}

}

web.xml:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

springmvc01

index.jsp

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

springDispatcherServlet

/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值