优化文件上传功能

This commit is contained in:
zhuangqian 2016-11-03 10:33:58 +08:00
parent 67ee233af1
commit c71c0735f4
14 changed files with 93 additions and 73 deletions

View File

@ -19,7 +19,7 @@
<shiro.version>1.3.2</shiro.version>
<log4j2.version>2.7</log4j2.version>
<beetl.version>2.7.0</beetl.version>
<beetlsql.version>2.6.6</beetlsql.version>
<beetlsql.version>2.6.7</beetlsql.version>
</properties>
<dependencies>

View File

@ -36,8 +36,18 @@ druid.maxWait = 60000
############################# config start ###########################################
#静态资源地址
config.basePath = /blade
#静态资源地址(static/image等推荐放入nginx)
config.domain = http://localhost:8080/blade
#config.domain = http://localhost
#远程上传模式
config.remoteMode = false
#上传头文件夹
config.uploadPath = /upload
#下载头文件夹
config.downloadPath = /download
#开发模式
config.devMode = true

View File

@ -36,8 +36,20 @@ druid.maxWait = 60000
############################# config start ###########################################
#静态资源地址
config.basePath = /blade
#静态资源地址(static/image等推荐放入nginx)
config.domain = http://localhost:8888/images
#远程上传模式
config.remoteMode = true
#远程上传地址(放在nginx)
config.remotePath = D://nginx/html/images
#上传头文件夹
config.uploadPath = /upload
#下载头文件夹
config.downloadPath = /download
#开发模式
config.devMode = false

View File

@ -15,12 +15,12 @@
*/
package com.smallchill.common.config;
import com.smallchill.common.intercept.DefaultCURDFactory;
import com.smallchill.common.intercept.DefaultSelectFactory;
import com.smallchill.common.plugins.GlobalPlugin;
import com.smallchill.core.constant.Cst;
import com.smallchill.core.interfaces.IConfig;
import com.smallchill.core.interfaces.IPluginFactroy;
import com.smallchill.core.shiro.DefaultShiroFactroy;
import com.smallchill.core.toolbox.grid.JqGridFactory;
import com.smallchill.core.toolbox.kit.DateKit;
import com.smallchill.core.toolbox.kit.Prop;
import com.smallchill.core.toolbox.kit.PropKit;
@ -37,17 +37,22 @@ public class WebConfig implements IConfig {
me.setDevMode(prop.getBoolean("config.devMode", false));
//设定文件上传是否为远程模式
me.setRemoteMode(false);
me.setRemoteMode(prop.getBoolean("config.remoteMode", false));
//远程上传地址
me.setRemotePath(prop.get("config.remotePath"));
//设定文件上传头文件夹
me.setUploadPath("/upload");
me.setUploadPath(prop.get("config.uploadPath"));
//设定文件下载头文件夹
me.setDownloadPath("/download");
me.setDownloadPath(prop.get("config.downloadPath"));
me.setDefaultCURDFactory(new DefaultCURDFactory());
//设定grid
me.setDefaultGridFactory(new JqGridFactory());
me.setDefaultSelectFactory(new DefaultSelectFactory());
//设定shiro工厂类
me.setDefaultShiroFactory(new DefaultShiroFactroy());
}
/**

View File

@ -165,17 +165,18 @@ public class BladeController implements ConstCurd, ConstCache, ConstCacheKey {
* @return
*/
public BladeFile getFile(MultipartFile file){
return getFile(file, null);
return getFile(file, null, null);
}
/**
* 获取BladeFile封装类
* @param file
* @param path
* @param virtualPath
* @return
*/
public BladeFile getFile(MultipartFile file, String path){
return new BladeFile(file, path);
public BladeFile getFile(MultipartFile file, String path, String virtualPath){
return new BladeFile(file, path, virtualPath);
}
/**
@ -184,19 +185,20 @@ public class BladeController implements ConstCurd, ConstCache, ConstCacheKey {
* @return
*/
public List<BladeFile> getFiles(List<MultipartFile> files){
return getFiles(files, null);
return getFiles(files, null, null);
}
/**
* 获取BladeFile封装类
* @param files
* @param path
* @param virtualPath
* @return
*/
public List<BladeFile> getFiles(List<MultipartFile> files, String path){
public List<BladeFile> getFiles(List<MultipartFile> files, String path, String virtualPath){
List<BladeFile> list = new ArrayList<>();
for (MultipartFile file : files){
list.add(new BladeFile(file, path));
list.add(new BladeFile(file, path, virtualPath));
}
return list;
}

View File

@ -68,7 +68,7 @@ public class BeetlTemplate {
public static void registerTemplate(GroupTemplate groupTemplate){
Map<String, Object> sharedVars = new HashMap<String, Object>();
sharedVars.put("startTime", new Date());
sharedVars.put("basePath", ConstConfig.BASEPATH);
sharedVars.put("domain", ConstConfig.DOMAIN);
groupTemplate.setSharedVars(sharedVars);
groupTemplate.registerTag("hot", HotBlogsTag.class);

View File

@ -14,6 +14,6 @@ public interface ConstConfig {
String MINIDLE = ConfigListener.map.get("druid.minIdle");
String MAXWAIT = ConfigListener.map.get("druid.maxWait");
String BASEPATH = ConfigListener.map.get("config.basePath");
String DOMAIN = ConfigListener.map.get("config.domain");
}

View File

@ -66,11 +66,11 @@ public class BladeFile {
this.uploadVirtualPath = Cst.me().getUploadCtxPath().replace(Cst.me().getContextPath(), "") + File.separator + DateKit.getDays() + File.separator + this.originalFileName;
}
public BladeFile(MultipartFile file, String uploadPath) {
public BladeFile(MultipartFile file, String uploadPath, String uploadVirtualPath) {
this(file);
if (null != uploadPath){
this.uploadPath = uploadPath;
this.uploadVirtualPath = null;
this.uploadVirtualPath = uploadVirtualPath;
}
}
@ -84,13 +84,10 @@ public class BladeFile {
File file = new File(uploadPath);
if(null != fileFactory){
this.uploadPath = fileFactory.path(file);
if(Cst.me().isRemoteMode()){
this.uploadVirtualPath = null;
} else{
this.uploadVirtualPath = fileFactory.virtualPath(file).replace(Cst.me().getContextPath(), "");
}
file = fileFactory.rename(file);
String [] path = fileFactory.path(file);
this.uploadPath = path[0];
this.uploadVirtualPath = path[1].replace(Cst.me().getContextPath(), "");
file = fileFactory.rename(path[0], file);
}
File dir = file.getParentFile();

View File

@ -28,30 +28,30 @@ import com.smallchill.system.model.Attach;
public class DefaultFileProxyFactory implements IFileProxy {
@Override
public File rename(File f) {
File dest = new File(path(f));
public File rename(String path, File f) {
File dest = new File(path);
f.renameTo(dest);
return dest;
}
@Override
public String path(File f) {
StringBuilder newFileName = new StringBuilder().append(File.separator)
public String [] path(File f) {
//避免网络延迟导致时间不同步
long time = System.currentTimeMillis();
StringBuilder uploadPath = new StringBuilder().append(File.separator)
.append(getFileDir(Cst.me().getUploadRealPath()))
.append(System.currentTimeMillis())
.append(time)
.append(getFileExt(f.getName()));
return newFileName.toString();
}
@Override
public String virtualPath(File f) {
StringBuilder newFileName = new StringBuilder()
StringBuilder virtualPath = new StringBuilder()
.append(getFileDir(Cst.me().getUploadCtxPath()))
.append(System.currentTimeMillis())
.append(time)
.append(getFileExt(f.getName()));
return newFileName.toString();
return new String [] {uploadPath.toString(), virtualPath.toString()};
}
@Override
public Object getFileId(BladeFile bf) {
Attach attach = new Attach();
@ -59,7 +59,7 @@ public class DefaultFileProxyFactory implements IFileProxy {
attach.setCreatetime(new Date());
attach.setName(bf.getOriginalFileName());
attach.setStatus(1);
attach.setUrl((Cst.me().isRemoteMode() ? bf.getUploadPath() : bf.getUploadVirtualPath()));
attach.setUrl(bf.getUploadVirtualPath());
return Blade.create(Attach.class).saveRtStrId(attach);
}

View File

@ -36,16 +36,12 @@ public class FileProxyManager {
this.defaultFileProxyFactory = defaultFileProxyFactory;
}
public String path(File file) {
public String [] path(File file) {
return defaultFileProxyFactory.path(file);
}
public String virtualPath(File file) {
return defaultFileProxyFactory.virtualPath(file);
}
public File rename(File file) {
return defaultFileProxyFactory.rename(file);
public File rename(String path, File file) {
return defaultFileProxyFactory.rename(path, file);
}
}

View File

@ -20,25 +20,18 @@ import java.io.File;
public interface IFileProxy {
/**
* 返回物理路径
* 返回路径[物理路径][虚拟路径]
* @param file
* @return
*/
String path(File file);
/**
* 返回相对路径
* @param file
* @return
*/
String virtualPath(File file);
String [] path(File file);
/**
* 文件重命名策略
* @param file
* @return
*/
File rename(File file);
File rename(String path, File f);
/**
* 获取入库id

View File

@ -63,9 +63,11 @@ public class GenerateController extends CurdController<Generate> {
public String createBuiltInSql(@PathVariable String table) {
try {
LogKit.println("\n\n-------------------------------- gen by beetlsql {} --------------------------------\n", DateKit.getTime());
LogKit.println("------------ curd ------------\n");
LogKit.println("------------ curd ------------\n");
Blade.dao().genBuiltInSqlToConsole(ClassKit.newInstance(table).getClass());
LogKit.println("\n------------ field ------------\n");
LogKit.println("\n-----↓-- updateNotNull --↓-----\n");
LogKit.println(Blade.dao().getDbStyle().genUpdateTemplate(ClassKit.newInstance(table).getClass()).getTemplate());
LogKit.println("\n-----↓------- field -------↓-----\n");
Blade.dao().genSQLTemplateToConsole(ClassKit.newInstance(table).getClass().getAnnotation(Table.class).name());
return "[ " + table + " ] sql生成成功,请查看控制台";
} catch (Exception e) {
@ -78,9 +80,11 @@ public class GenerateController extends CurdController<Generate> {
public String createBuiltInSqlSlave(@PathVariable String slave, @PathVariable String table) {
try {
LogKit.println("\n\n-------------------------------- gen by beetlsql {} --------------------------------\n", DateKit.getTime());
LogKit.println("------------ curd ------------\n");
LogKit.println("------------ curd --------↓-----\n");
Blade.dao(slave).genBuiltInSqlToConsole(ClassKit.newInstance(table).getClass());
LogKit.println("\n------------ field ------------\n");
LogKit.println("\n-----↓-- updateNotNull --↓-----\n");
LogKit.println(Blade.dao(slave).getDbStyle().genUpdateTemplate(ClassKit.newInstance(table).getClass()).getTemplate());
LogKit.println("\n-----↓------ field -------↓-----\n");
Blade.dao(slave).genSQLTemplateToConsole(ClassKit.newInstance(table).getClass().getAnnotation(Table.class).name());
return "[ " + table + " ] sql生成成功,请查看控制台";
} catch (Exception e) {

View File

@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.smallchill.core.base.controller.BladeController;
import com.smallchill.core.constant.ConstConfig;
import com.smallchill.core.constant.Cst;
import com.smallchill.core.plugins.dao.Db;
import com.smallchill.core.toolbox.Paras;
@ -64,10 +65,10 @@ public class KindEditorController extends BladeController {
BladeFile bf = getFile(file);
bf.transfer();
Object fileId = bf.getFileId();
String url = "/kindeditor/renderFile/" + fileId;
String url = ConstConfig.DOMAIN + bf.getUploadVirtualPath();
rd.set("error", 0);
rd.set("title", fileId);
rd.set("url", Cst.me().getContextPath() + url);
rd.set("url", url);
rd.set("name", originalFileName);
return rd;
}

View File

@ -21,6 +21,7 @@ import java.util.Map;
import com.smallchill.common.tool.SysCache;
import com.smallchill.core.aop.AopContext;
import com.smallchill.core.constant.ConstConfig;
import com.smallchill.core.constant.Cst;
import com.smallchill.core.meta.MetaIntercept;
import com.smallchill.core.plugins.dao.Db;
@ -39,7 +40,7 @@ public class AttachIntercept extends MetaIntercept {
BladePage<Map<String, Object>> page = (BladePage<Map<String, Object>>) ac.getObject();
List<Map<String, Object>> list = page.getRows();
for (Map<String, Object> map : list) {
map.put("ATTACHURL", Cst.me().getContextPath() + "/kindeditor/renderFile/" + map.get("ID"));
map.put("ATTACHURL", ConstConfig.DOMAIN + map.get("URL"));
map.put("STATUSNAME", SysCache.getDictName(902, map.get("STATUS")));
map.put("CREATERNAME", SysCache.getUserName(map.get("CREATER")));
}
@ -51,11 +52,10 @@ public class AttachIntercept extends MetaIntercept {
* @param ac
*/
public void renderViewBefore(AopContext ac) {
Paras rd = (Paras) ac.getObject();
rd
.set("attachUrl", Cst.me().getContextPath() + "/kindeditor/renderFile/" + rd.get("id"))
.set("statusName", SysCache.getDictName(902, rd.get("status")))
.set("createrName", SysCache.getUserName(rd.get("creater")));
Paras ps = (Paras) ac.getObject();
ps.set("attachUrl", ConstConfig.DOMAIN + ps.get("url"))
.set("statusName", SysCache.getDictName(902, ps.get("status")))
.set("createrName", SysCache.getUserName(ps.get("creater")));
}
/**