mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
图片上传流程优化
This commit is contained in:
parent
9de47ce697
commit
9d2c453bb0
@ -30,9 +30,9 @@ public class ResultBean<T> implements Serializable {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResultBean(ResponseStatus ResponseStatus) {
|
private ResultBean(ResponseStatus responseStatus) {
|
||||||
this.code = ResponseStatus.getCode();;
|
this.code = responseStatus.getCode();;
|
||||||
this.msg = ResponseStatus.getMsg();
|
this.msg = responseStatus.getMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResultBean(T data) {
|
private ResultBean(T data) {
|
||||||
@ -44,30 +44,30 @@ public class ResultBean<T> implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 业务处理成功,无数据返回
|
* 业务处理成功,无数据返回
|
||||||
* */
|
* */
|
||||||
public static ResultBean ok() {
|
public static ResultBean<Void> ok() {
|
||||||
return new ResultBean();
|
return new ResultBean<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 业务处理成功,有数据返回
|
* 业务处理成功,有数据返回
|
||||||
* */
|
* */
|
||||||
public static <T> ResultBean ok(T data) {
|
public static <T> ResultBean<T> ok(T data) {
|
||||||
return new ResultBean(data);
|
return new ResultBean<>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 业务处理失败
|
* 业务处理失败
|
||||||
* */
|
* */
|
||||||
public static ResultBean fail(ResponseStatus ResponseStatus) {
|
public static ResultBean<Void> fail(ResponseStatus responseStatus) {
|
||||||
return new ResultBean(ResponseStatus);
|
return new ResultBean<>(responseStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 系统错误
|
* 系统错误
|
||||||
* */
|
* */
|
||||||
public static ResultBean error() {
|
public static ResultBean<Void> error() {
|
||||||
return new ResultBean(ResponseStatus.ERROR);
|
return new ResultBean<>(ResponseStatus.ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +68,12 @@ public enum ResponseStatus {
|
|||||||
* */
|
* */
|
||||||
ES_SEARCH_FAIL(9001,"搜索引擎查询错误!"),
|
ES_SEARCH_FAIL(9001,"搜索引擎查询错误!"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件相关错误
|
||||||
|
* */
|
||||||
|
FILE_DIR_MAKE_FAIL(10001,"目录创建失败"),
|
||||||
|
FILE_NOT_IMAGE(10002,"请上传图片类型的文件"),
|
||||||
|
FILE_SIZE_LIMIT(10003,"文件大小超出限制"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 其他通用错误
|
* 其他通用错误
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.java2nb.novel.core.utils;
|
package com.java2nb.novel.core.utils;
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.codec.Charsets;
|
import org.apache.commons.codec.Charsets;
|
||||||
import org.apache.http.client.utils.DateUtils;
|
import org.apache.http.client.utils.DateUtils;
|
||||||
@ -11,6 +12,7 @@ import org.springframework.http.HttpMethod;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -19,13 +21,14 @@ import java.util.Objects;
|
|||||||
* 文件操作工具类
|
* 文件操作工具类
|
||||||
* @author 11797
|
* @author 11797
|
||||||
*/
|
*/
|
||||||
|
@UtilityClass
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网络图片转本地
|
* 网络图片转本地
|
||||||
* */
|
* */
|
||||||
public static String network2Local(String picSrc,String picSavePath,String visitPrefix) {
|
public String network2Local(String picSrc,String picSavePath,String visitPrefix) {
|
||||||
InputStream input = null;
|
InputStream input = null;
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
try {
|
try {
|
||||||
@ -82,5 +85,21 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否为图片
|
||||||
|
* @param file 需要判断的文件
|
||||||
|
* @return true:是图片,false:不是图片
|
||||||
|
* */
|
||||||
|
@SneakyThrows
|
||||||
|
public boolean isImage(File file){
|
||||||
|
|
||||||
|
BufferedImage bi = ImageIO.read(file);
|
||||||
|
|
||||||
|
return bi != null;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,11 @@ spring:
|
|||||||
generator:
|
generator:
|
||||||
write-numbers-as-strings: true
|
write-numbers-as-strings: true
|
||||||
|
|
||||||
|
#上传文件的最大值(1M)
|
||||||
|
servlet:
|
||||||
|
multipart:
|
||||||
|
max-file-size: 1048576
|
||||||
|
|
||||||
#缓存类型,ehcache(默认)、redis
|
#缓存类型,ehcache(默认)、redis
|
||||||
cache:
|
cache:
|
||||||
type: ehcache
|
type: ehcache
|
||||||
@ -26,3 +31,5 @@ logging:
|
|||||||
config: classpath:logback-boot.xml
|
config: classpath:logback-boot.xml
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,28 +2,23 @@ package com.java2nb.novel.controller;
|
|||||||
|
|
||||||
import com.java2nb.novel.core.bean.ResultBean;
|
import com.java2nb.novel.core.bean.ResultBean;
|
||||||
import com.java2nb.novel.core.cache.CacheService;
|
import com.java2nb.novel.core.cache.CacheService;
|
||||||
|
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||||
|
import com.java2nb.novel.core.exception.BusinessException;
|
||||||
import com.java2nb.novel.core.utils.Constants;
|
import com.java2nb.novel.core.utils.Constants;
|
||||||
|
import com.java2nb.novel.core.utils.FileUtil;
|
||||||
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
|
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
|
||||||
import com.java2nb.novel.core.utils.RestTemplateUtil;
|
|
||||||
import com.java2nb.novel.core.utils.UUIDUtil;
|
import com.java2nb.novel.core.utils.UUIDUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.codec.Charsets;
|
|
||||||
import org.apache.http.client.utils.DateUtils;
|
import org.apache.http.client.utils.DateUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.Resource;
|
|
||||||
import org.springframework.http.HttpEntity;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -63,29 +58,35 @@ public class FileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 图片上传
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@SneakyThrows
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@PostMapping("/upload")
|
@PostMapping("/picUpload")
|
||||||
ResultBean upload(@RequestParam("file") MultipartFile file) {
|
ResultBean<String> upload(@RequestParam("file") MultipartFile file) {
|
||||||
Date currentDate = new Date();
|
Date currentDate = new Date();
|
||||||
try {
|
String savePath =
|
||||||
String savePath =
|
Constants.LOCAL_PIC_PREFIX + DateUtils.formatDate(currentDate, "yyyy") + "/" +
|
||||||
Constants.LOCAL_PIC_PREFIX + DateUtils.formatDate(currentDate, "yyyy") + "/" +
|
DateUtils.formatDate(currentDate, "MM") + "/" +
|
||||||
DateUtils.formatDate(currentDate, "MM") + "/" +
|
DateUtils.formatDate(currentDate, "dd");
|
||||||
DateUtils.formatDate(currentDate, "dd") ;
|
String oriName = file.getOriginalFilename();
|
||||||
String oriName = file.getOriginalFilename();
|
assert oriName != null;
|
||||||
String saveFileName = UUIDUtil.getUUID32() + oriName.substring(oriName.lastIndexOf("."));
|
String saveFileName = UUIDUtil.getUUID32() + oriName.substring(oriName.lastIndexOf("."));
|
||||||
File saveFile = new File( picSavePath + savePath, saveFileName);
|
File saveFile = new File(picSavePath + savePath, saveFileName);
|
||||||
if (!saveFile.getParentFile().exists()) {
|
if (!saveFile.getParentFile().exists()) {
|
||||||
saveFile.getParentFile().mkdirs();
|
boolean isSuccess = saveFile.getParentFile().mkdirs();
|
||||||
|
if(!isSuccess){
|
||||||
|
throw new BusinessException(ResponseStatus.FILE_DIR_MAKE_FAIL);
|
||||||
}
|
}
|
||||||
file.transferTo(saveFile);
|
|
||||||
return ResultBean.ok(savePath+"/"+saveFileName);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
return ResultBean.error();
|
|
||||||
}
|
}
|
||||||
|
file.transferTo(saveFile);
|
||||||
|
if(!FileUtil.isImage(saveFile)){
|
||||||
|
//上传的文件不是图片
|
||||||
|
saveFile.delete();
|
||||||
|
throw new BusinessException(ResponseStatus.FILE_NOT_IMAGE);
|
||||||
|
};
|
||||||
|
return ResultBean.ok(savePath + "/" + saveFileName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,3 +125,35 @@ function logout() {
|
|||||||
$.cookie('Authorization', null,{ path: '/' });
|
$.cookie('Authorization', null,{ path: '/' });
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function isImg(str) {
|
||||||
|
return !str.search("[.]+(jpg|jpeg|swf|gif|png|JPG|JPEG|SWF|GIF|PNG)$");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//校验图片上传
|
||||||
|
function checkPicUpload(file){
|
||||||
|
|
||||||
|
if(!isImg(file.value.substr(file.value.lastIndexOf(".")))){
|
||||||
|
layer.alert('只能上传图片格式的文件!');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var fileSize = 0;
|
||||||
|
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
|
||||||
|
if (isIE && !file.files) {
|
||||||
|
var filePath = file.value;
|
||||||
|
var fileSystem = new ActiveXObject("Scripting.FileSystemfileect");
|
||||||
|
var file = fileSystem.GetFile (filePath);
|
||||||
|
fileSize = file.Size;
|
||||||
|
}else {
|
||||||
|
fileSize = file.files[0].size;
|
||||||
|
}
|
||||||
|
fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
|
||||||
|
if(fileSize>=1024){
|
||||||
|
layer.alert('上传的图片大小不能超过1M!');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -142,29 +142,32 @@
|
|||||||
<script src="/layui/layui.all.js" type="text/javascript"></script>
|
<script src="/layui/layui.all.js" type="text/javascript"></script>
|
||||||
<script src="/javascript/header.js" type="text/javascript"></script>
|
<script src="/javascript/header.js" type="text/javascript"></script>
|
||||||
<script src="/javascript/user.js" type="text/javascript"></script>
|
<script src="/javascript/user.js" type="text/javascript"></script>
|
||||||
|
<script src="/javascript/common.js" type="text/javascript"></script>
|
||||||
<script language="javascript" type="text/javascript">
|
<script language="javascript" type="text/javascript">
|
||||||
|
|
||||||
function picChange() {
|
function picChange() {
|
||||||
var file = $("#file0").val(); //文件名称
|
var file = $("#file0").val(); //文件名称
|
||||||
if (file != "") {
|
if (file != "") {
|
||||||
|
|
||||||
$.ajaxFileUpload({
|
if(checkPicUpload($("#file0")[0])) {
|
||||||
url : "/file/upload", //用于文件上传的服务器端请求地址
|
$.ajaxFileUpload({
|
||||||
secureuri : false, //是否需要安全协议,一般设置为false
|
url: "/file/picUpload", //用于文件上传的服务器端请求地址
|
||||||
fileElementId : "file0", //文件上传域的ID
|
secureuri: false, //是否需要安全协议,一般设置为false
|
||||||
dataType : "json", //返回值类型 一般设置为json
|
fileElementId: "file0", //文件上传域的ID
|
||||||
type : "post",
|
dataType: "json", //返回值类型 一般设置为json
|
||||||
success : function(data) { //服务器成功响应处理函数
|
type: "post",
|
||||||
if (data.code == 200) {
|
success: function (data) { //服务器成功响应处理函数
|
||||||
$("#picImage").attr("src", data.data);
|
if (data.code == 200) {
|
||||||
$("#picUrl").val(data.data);
|
$("#picImage").attr("src", data.data);
|
||||||
}else {
|
$("#picUrl").val(data.data);
|
||||||
layer.alert('图片上传失败');
|
} else {
|
||||||
|
layer.alert(data.msg);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
});
|
||||||
|
}
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
alert("请选择上传文件!");
|
alert("请选择上传文件!");
|
||||||
}
|
}
|
||||||
|
@ -138,6 +138,7 @@
|
|||||||
<script src="/javascript/header.js" type="text/javascript"></script>
|
<script src="/javascript/header.js" type="text/javascript"></script>
|
||||||
<script src="/javascript/user.js" type="text/javascript"></script>
|
<script src="/javascript/user.js" type="text/javascript"></script>
|
||||||
<script src="/javascript/date.js" type="text/javascript"></script>
|
<script src="/javascript/date.js" type="text/javascript"></script>
|
||||||
|
<script src="/javascript/common.js" type="text/javascript"></script>
|
||||||
|
|
||||||
<script language="javascript" type="text/javascript">
|
<script language="javascript" type="text/javascript">
|
||||||
search(1, 5);
|
search(1, 5);
|
||||||
@ -281,44 +282,46 @@
|
|||||||
function picChange(bookId) {
|
function picChange(bookId) {
|
||||||
var file = $("#file0").val(); //文件名称
|
var file = $("#file0").val(); //文件名称
|
||||||
if (file != "") {
|
if (file != "") {
|
||||||
|
if(checkPicUpload($("#file0")[0])) {
|
||||||
|
|
||||||
$.ajaxFileUpload({
|
$.ajaxFileUpload({
|
||||||
url : "/file/upload", //用于文件上传的服务器端请求地址
|
url: "/file/picUpload", //用于文件上传的服务器端请求地址
|
||||||
secureuri : false, //是否需要安全协议,一般设置为false
|
secureuri: false, //是否需要安全协议,一般设置为false
|
||||||
fileElementId : "file0", //文件上传域的ID
|
fileElementId: "file0", //文件上传域的ID
|
||||||
dataType : "json", //返回值类型 一般设置为json
|
dataType: "json", //返回值类型 一般设置为json
|
||||||
type : "post",
|
type: "post",
|
||||||
success : function(data) { //服务器成功响应处理函数
|
success: function (data) { //服务器成功响应处理函数
|
||||||
if (data.code == 200) {
|
if (data.code == 200) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: "/author/updateBookPic",
|
url: "/author/updateBookPic",
|
||||||
data: {'bookId':bookId,'bookPic':data.data},
|
data: {'bookId': bookId, 'bookPic': data.data},
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
if (data.code == 200) {
|
if (data.code == 200) {
|
||||||
|
|
||||||
location.reload();
|
location.reload();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
lock = false;
|
||||||
|
layer.alert(data.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
lock = false;
|
lock = false;
|
||||||
layer.alert(data.msg);
|
layer.alert('网络异常');
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
},
|
} else {
|
||||||
error: function () {
|
layer.alert(data.msg);
|
||||||
lock = false;
|
}
|
||||||
layer.alert('网络异常');
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}else {
|
|
||||||
layer.alert('图片上传失败');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
});
|
||||||
|
}
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
alert("请选择上传文件!");
|
alert("请选择上传文件!");
|
||||||
}
|
}
|
||||||
|
@ -96,45 +96,48 @@
|
|||||||
var file = $("#file0").val(); //文件名称
|
var file = $("#file0").val(); //文件名称
|
||||||
if (file != "") {
|
if (file != "") {
|
||||||
|
|
||||||
$.ajaxFileUpload({
|
if(checkPicUpload($("#file0")[0])) {
|
||||||
url : "/file/upload", //用于文件上传的服务器端请求地址
|
|
||||||
secureuri : false, //是否需要安全协议,一般设置为false
|
|
||||||
fileElementId : "file0", //文件上传域的ID
|
|
||||||
dataType : "json", //返回值类型 一般设置为json
|
|
||||||
type : "post",
|
|
||||||
success : function(data) { //服务器成功响应处理函数
|
|
||||||
if (data.code == 200) {
|
|
||||||
|
|
||||||
$.ajax({
|
$.ajaxFileUpload({
|
||||||
type: "POST",
|
url: "/file/picUpload", //用于文件上传的服务器端请求地址
|
||||||
url: "/user/updateUserInfo",
|
secureuri: false, //是否需要安全协议,一般设置为false
|
||||||
data: {'userPhoto':data.data},
|
fileElementId: "file0", //文件上传域的ID
|
||||||
dataType: "json",
|
dataType: "json", //返回值类型 一般设置为json
|
||||||
success: function (data) {
|
type: "post",
|
||||||
if (data.code == 200) {
|
success: function (data) { //服务器成功响应处理函数
|
||||||
window.location.href = '/user/setup.html';
|
if (data.code == 200) {
|
||||||
|
|
||||||
} else if (data.code == 1001) {
|
$.ajax({
|
||||||
//未登录
|
type: "POST",
|
||||||
location.href = '/user/login.html?originUrl=' + decodeURIComponent(location.href);
|
url: "/user/updateUserInfo",
|
||||||
|
data: {'userPhoto': data.data},
|
||||||
|
dataType: "json",
|
||||||
|
success: function (data) {
|
||||||
|
if (data.code == 200) {
|
||||||
|
window.location.href = '/user/setup.html';
|
||||||
|
|
||||||
} else {
|
} else if (data.code == 1001) {
|
||||||
layer.alert(data.msg);
|
//未登录
|
||||||
|
location.href = '/user/login.html?originUrl=' + decodeURIComponent(location.href);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
layer.alert(data.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
error: function () {
|
||||||
|
layer.alert('网络异常');
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
},
|
} else {
|
||||||
error: function () {
|
layer.alert(data.msg);
|
||||||
layer.alert('网络异常');
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}else {
|
|
||||||
layer.alert('图片上传失败');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
});
|
||||||
|
}
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
alert("请选择上传文件!");
|
alert("请选择上传文件!");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user