mirror of
https://github.com/201206030/novel-plus.git
synced 2025-04-27 01:30:51 +00:00
小说封面图片上传
This commit is contained in:
parent
9d621edaec
commit
70f04bd37c
@ -1,8 +0,0 @@
|
||||
2020-05-13 21:52:00,972 INFO (StartupInfoLogger.java:50)- Starting TestDemo on USER-20180729KA with PID 3532 (started by Administrator in E:\baseprojectparent\novel-plus\novel-admin)
|
||||
2020-05-13 21:52:01,113 DEBUG (StartupInfoLogger.java:53)- Running with Spring Boot v2.0.1.RELEASE, Spring v5.0.5.RELEASE
|
||||
2020-05-13 21:52:01,131 INFO (SpringApplication.java:663)- The following profiles are active: dev
|
||||
2020-05-13 21:52:54,469 DEBUG (ApplicationContextRegister.java:29)- ApplicationContext registed-->org.springframework.web.context.support.GenericWebApplicationContext@5b529706: startup date [Wed May 13 21:52:01 CST 2020]; root of context hierarchy
|
||||
2020-05-13 21:53:49,622 INFO (StartupInfoLogger.java:59)- Started TestDemo in 114.268 seconds (JVM running for 124.957)
|
||||
2020-05-18 09:48:03,219 INFO (StartupInfoLogger.java:50)- Starting TestDemo on DESKTOP-CPCLUI6 with PID 13172 (started by 11797 in D:\gitee\novel-plus\novel-admin)
|
||||
2020-05-18 09:48:03,223 DEBUG (StartupInfoLogger.java:53)- Running with Spring Boot v2.0.1.RELEASE, Spring v5.0.5.RELEASE
|
||||
2020-05-18 09:48:03,227 INFO (SpringApplication.java:663)- The following profiles are active: dev
|
@ -1,17 +1,32 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.java2nb.novel.core.bean.ResultBean;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.utils.Constants;
|
||||
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
|
||||
import com.java2nb.novel.core.utils.RestTemplateUtil;
|
||||
import com.java2nb.novel.core.utils.UUIDUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.Charsets;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
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.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
@ -24,6 +39,9 @@ public class FileController {
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
@Value("${pic.save.path}")
|
||||
private String picSavePath;
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*/
|
||||
@ -44,5 +62,32 @@ public class FileController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping("/upload")
|
||||
ResultBean upload(@RequestParam("file") MultipartFile file) {
|
||||
Date currentDate = new Date();
|
||||
try {
|
||||
String savePath =
|
||||
Constants.LOCAL_PIC_PREFIX + DateUtils.formatDate(currentDate, "yyyy") + "/" +
|
||||
DateUtils.formatDate(currentDate, "MM") + "/" +
|
||||
DateUtils.formatDate(currentDate, "dd") ;
|
||||
String oriName = file.getOriginalFilename();
|
||||
String saveFileName = UUIDUtil.getUUID32() + oriName.substring(oriName.lastIndexOf("."));
|
||||
File saveFile = new File( picSavePath + savePath, saveFileName);
|
||||
if (!saveFile.getParentFile().exists()) {
|
||||
saveFile.getParentFile().mkdirs();
|
||||
}
|
||||
file.transferTo(saveFile);
|
||||
return ResultBean.ok(savePath+"/"+saveFileName);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return ResultBean.error();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,217 @@
|
||||
jQuery.extend({
|
||||
createUploadIframe: function(id, uri)
|
||||
{
|
||||
//create frame
|
||||
var frameId = 'jUploadFrame' + id;
|
||||
var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
|
||||
if(window.ActiveXObject)
|
||||
{
|
||||
if(typeof uri== 'boolean'){
|
||||
iframeHtml += ' src="' + 'javascript:false' + '"';
|
||||
|
||||
}
|
||||
else if(typeof uri== 'string'){
|
||||
iframeHtml += ' src="' + uri + '"';
|
||||
|
||||
}
|
||||
}
|
||||
iframeHtml += ' />';
|
||||
jQuery(iframeHtml).appendTo(document.body);
|
||||
|
||||
return jQuery('#' + frameId).get(0);
|
||||
},
|
||||
createUploadForm: function(id,fileElementId,data,fileElement)
|
||||
{
|
||||
//create form
|
||||
var formId = 'jUploadForm' + id;
|
||||
var fileId = 'jUploadFile' + id;
|
||||
var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
|
||||
if(data)
|
||||
{
|
||||
for(var i in data)
|
||||
{
|
||||
jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
|
||||
}
|
||||
}
|
||||
var oldElement;
|
||||
if(fileElement == null)
|
||||
oldElement = jQuery('#' + fileElementId);
|
||||
else
|
||||
oldElement = fileElement;
|
||||
|
||||
var newElement = jQuery(oldElement).clone();
|
||||
jQuery(oldElement).attr('id', fileId);
|
||||
jQuery(oldElement).before(newElement);
|
||||
jQuery(oldElement).appendTo(form);
|
||||
|
||||
//set attributes
|
||||
jQuery(form).css('position', 'absolute');
|
||||
jQuery(form).css('top', '-1200px');
|
||||
jQuery(form).css('left', '-1200px');
|
||||
jQuery(form).appendTo('body');
|
||||
return form;
|
||||
},
|
||||
|
||||
ajaxFileUpload: function(s) {
|
||||
// TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
|
||||
s = jQuery.extend({}, jQuery.ajaxSettings, s);
|
||||
var id = new Date().getTime()
|
||||
var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data),s.fileElement);
|
||||
var io = jQuery.createUploadIframe(id, s.secureuri);
|
||||
var frameId = 'jUploadFrame' + id;
|
||||
var formId = 'jUploadForm' + id;
|
||||
// Watch for a new set of requests
|
||||
if ( s.global && ! jQuery.active++ )
|
||||
{
|
||||
jQuery.event.trigger( "ajaxStart" );
|
||||
}
|
||||
var requestDone = false;
|
||||
// Create the request object
|
||||
var xml = {}
|
||||
if ( s.global )
|
||||
jQuery.event.trigger("ajaxSend", [xml, s]);
|
||||
// Wait for a response to come back
|
||||
var uploadCallback = function(isTimeout)
|
||||
{
|
||||
var io = document.getElementById(frameId);
|
||||
try
|
||||
{
|
||||
if(io.contentWindow)
|
||||
{
|
||||
xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
|
||||
xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
|
||||
|
||||
}else if(io.contentDocument)
|
||||
{
|
||||
xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
|
||||
xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
|
||||
}
|
||||
}catch(e)
|
||||
{
|
||||
jQuery.handleError(s, xml, null, e);
|
||||
}
|
||||
if ( xml || isTimeout == "timeout")
|
||||
{
|
||||
requestDone = true;
|
||||
var status;
|
||||
try {
|
||||
status = isTimeout != "timeout" ? "success" : "error";
|
||||
// Make sure that the request was successful or notmodified
|
||||
if ( status != "error" )
|
||||
{
|
||||
// process the data (runs the xml through httpData regardless of callback)
|
||||
var data = jQuery.uploadHttpData( xml, s.dataType );
|
||||
// If a local callback was specified, fire it and pass it the data
|
||||
if ( s.success )
|
||||
s.success( data, status );
|
||||
|
||||
// Fire the global callback
|
||||
if( s.global )
|
||||
jQuery.event.trigger( "ajaxSuccess", [xml, s] );
|
||||
} else
|
||||
jQuery.handleError(s, xml, status);
|
||||
} catch(e)
|
||||
{
|
||||
status = "error";
|
||||
jQuery.handleError(s, xml, status, e);
|
||||
}
|
||||
|
||||
// The request was completed
|
||||
if( s.global )
|
||||
jQuery.event.trigger( "ajaxComplete", [xml, s] );
|
||||
|
||||
// Handle the global AJAX counter
|
||||
if ( s.global && ! --jQuery.active )
|
||||
jQuery.event.trigger( "ajaxStop" );
|
||||
|
||||
// Process result
|
||||
if ( s.complete )
|
||||
s.complete(xml, status);
|
||||
|
||||
jQuery(io).unbind()
|
||||
|
||||
setTimeout(function()
|
||||
{ try
|
||||
{
|
||||
jQuery(io).remove();
|
||||
jQuery(form).remove();
|
||||
|
||||
} catch(e)
|
||||
{
|
||||
jQuery.handleError(s, xml, null, e);
|
||||
}
|
||||
|
||||
}, 100)
|
||||
|
||||
xml = null
|
||||
|
||||
}
|
||||
}
|
||||
// Timeout checker
|
||||
if ( s.timeout > 0 )
|
||||
{
|
||||
setTimeout(function(){
|
||||
// Check to see if the request is still happening
|
||||
if( !requestDone ) uploadCallback( "timeout" );
|
||||
}, s.timeout);
|
||||
}
|
||||
try
|
||||
{
|
||||
|
||||
var form = jQuery('#' + formId);
|
||||
jQuery(form).attr('action', s.url);
|
||||
jQuery(form).attr('method', 'POST');
|
||||
jQuery(form).attr('target', frameId);
|
||||
if(form.encoding)
|
||||
{
|
||||
jQuery(form).attr('encoding', 'multipart/form-data');
|
||||
}
|
||||
else
|
||||
{
|
||||
jQuery(form).attr('enctype', 'multipart/form-data');
|
||||
}
|
||||
jQuery(form).submit();
|
||||
|
||||
} catch(e)
|
||||
{
|
||||
jQuery.handleError(s, xml, null, e);
|
||||
}
|
||||
|
||||
jQuery('#' + frameId).load(uploadCallback);
|
||||
return {abort: function(){
|
||||
try
|
||||
{
|
||||
jQuery('#' + frameId).remove();
|
||||
jQuery(form).remove();
|
||||
}
|
||||
catch(e){}
|
||||
}};
|
||||
},
|
||||
|
||||
uploadHttpData: function( r, type ) {
|
||||
var data = !type;
|
||||
data = type == "xml" || data ? r.responseXML : r.responseText;
|
||||
|
||||
// If the type is "script", eval it in global context
|
||||
if ( type == "script" )
|
||||
jQuery.globalEval( data );
|
||||
// Get the JavaScript object, if JSON is used.
|
||||
if ( type == "json" )
|
||||
data = jQuery.parseJSON(jQuery(data).text());
|
||||
// evaluate scripts within html
|
||||
if ( type == "html" )
|
||||
jQuery("<div>").html(data).evalScripts();
|
||||
|
||||
return data;
|
||||
},
|
||||
|
||||
handleError: function( s, xml, status, e ) {
|
||||
// If a local callback was specified, fire it
|
||||
if ( s.error )
|
||||
s.error( xml, status, e );
|
||||
|
||||
// Fire the global callback
|
||||
if ( s.global )
|
||||
jQuery.event.trigger( "ajaxError", [xml, s, e] );
|
||||
}
|
||||
});
|
@ -8,6 +8,9 @@
|
||||
<title>作家管理系统-小说精品屋</title>
|
||||
<link rel="stylesheet" href="/css/base.css?v=1"/>
|
||||
<link rel="stylesheet" href="/css/user.css"/>
|
||||
<style type="text/css">
|
||||
.opacity{-khtml-opacity:0.0;-moz-opacity:0.0;filter:alpha(opacity=0);filter:"alpha(opacity=0)";opacity:0.0; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);/*兼容ie8及以下*/}
|
||||
</style>
|
||||
</head>
|
||||
</head>
|
||||
<body class="">
|
||||
@ -74,8 +77,15 @@
|
||||
<input type="hidden" id="catName" name="catName" value="玄幻奇幻"/>
|
||||
<b>小说名:</b>
|
||||
<li><input type="text" id="bookName" name="bookName" class="s_input" ></li>
|
||||
<b>小说封面图片路径:</b>
|
||||
<li><input type="text" id="picUrl" name="picUrl" class="s_input" ></li>
|
||||
<b>小说封面:</b>
|
||||
<li style="position: relative">
|
||||
<input class="opacity" onchange="picChange()"
|
||||
type="file" id="file0" name="file"
|
||||
title="点击更换图片"
|
||||
style="z-index: 100;cursor: pointer;left: 0px; top: 0px; width: 100px; height: 130px; opacity: 0; position: absolute; "
|
||||
/>
|
||||
<img style="width:100px;height: 130px" id="picImage" src="/images/default.gif" alt="">
|
||||
<input type="hidden" id="picUrl" name="picUrl" class="s_input" value="/images/default.gif"></li>
|
||||
<b>小说介绍:</b>
|
||||
|
||||
<li><textarea name="bookDesc" rows="5" cols="53" id="bookDesc"
|
||||
@ -126,11 +136,42 @@
|
||||
|
||||
</body>
|
||||
<script src="/javascript/jquery-1.8.0.min.js" type="text/javascript"></script>
|
||||
<script src="/javascript/ajaxfileupload.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/user.js" type="text/javascript"></script>
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
function picChange() {
|
||||
var file = $("#file0").val(); //文件名称
|
||||
if (file != "") {
|
||||
|
||||
$.ajaxFileUpload({
|
||||
url : "/file/upload", //用于文件上传的服务器端请求地址
|
||||
secureuri : false, //是否需要安全协议,一般设置为false
|
||||
fileElementId : "file0", //文件上传域的ID
|
||||
dataType : "json", //返回值类型 一般设置为json
|
||||
type : "post",
|
||||
success : function(data) { //服务器成功响应处理函数
|
||||
if (data.code == 200) {
|
||||
$("#picImage").attr("src", data.data);
|
||||
$("#picUrl").val(data.data);
|
||||
}else {
|
||||
layer.alert('图片上传失败');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
} else {
|
||||
alert("请选择上传文件!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
var lock = false;
|
||||
function addBook() {
|
||||
if(lock){
|
||||
|
Loading…
x
Reference in New Issue
Block a user