优化文件上传功能

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> <shiro.version>1.3.2</shiro.version>
<log4j2.version>2.7</log4j2.version> <log4j2.version>2.7</log4j2.version>
<beetl.version>2.7.0</beetl.version> <beetl.version>2.7.0</beetl.version>
<beetlsql.version>2.6.6</beetlsql.version> <beetlsql.version>2.6.7</beetlsql.version>
</properties> </properties>
<dependencies> <dependencies>

View File

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

View File

@ -36,8 +36,20 @@ druid.maxWait = 60000
############################# config start ########################################### ############################# config start ###########################################
#静态资源地址 #静态资源地址(static/image等推荐放入nginx)
config.basePath = /blade 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 config.devMode = false

View File

@ -15,12 +15,12 @@
*/ */
package com.smallchill.common.config; 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.common.plugins.GlobalPlugin;
import com.smallchill.core.constant.Cst; import com.smallchill.core.constant.Cst;
import com.smallchill.core.interfaces.IConfig; import com.smallchill.core.interfaces.IConfig;
import com.smallchill.core.interfaces.IPluginFactroy; 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.DateKit;
import com.smallchill.core.toolbox.kit.Prop; import com.smallchill.core.toolbox.kit.Prop;
import com.smallchill.core.toolbox.kit.PropKit; import com.smallchill.core.toolbox.kit.PropKit;
@ -37,17 +37,22 @@ public class WebConfig implements IConfig {
me.setDevMode(prop.getBoolean("config.devMode", false)); 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 * @return
*/ */
public BladeFile getFile(MultipartFile file){ public BladeFile getFile(MultipartFile file){
return getFile(file, null); return getFile(file, null, null);
} }
/** /**
* 获取BladeFile封装类 * 获取BladeFile封装类
* @param file * @param file
* @param path * @param path
* @param virtualPath
* @return * @return
*/ */
public BladeFile getFile(MultipartFile file, String path){ public BladeFile getFile(MultipartFile file, String path, String virtualPath){
return new BladeFile(file, path); return new BladeFile(file, path, virtualPath);
} }
/** /**
@ -184,19 +185,20 @@ public class BladeController implements ConstCurd, ConstCache, ConstCacheKey {
* @return * @return
*/ */
public List<BladeFile> getFiles(List<MultipartFile> files){ public List<BladeFile> getFiles(List<MultipartFile> files){
return getFiles(files, null); return getFiles(files, null, null);
} }
/** /**
* 获取BladeFile封装类 * 获取BladeFile封装类
* @param files * @param files
* @param path * @param path
* @param virtualPath
* @return * @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<>(); List<BladeFile> list = new ArrayList<>();
for (MultipartFile file : files){ for (MultipartFile file : files){
list.add(new BladeFile(file, path)); list.add(new BladeFile(file, path, virtualPath));
} }
return list; return list;
} }

View File

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

View File

@ -14,6 +14,6 @@ public interface ConstConfig {
String MINIDLE = ConfigListener.map.get("druid.minIdle"); String MINIDLE = ConfigListener.map.get("druid.minIdle");
String MAXWAIT = ConfigListener.map.get("druid.maxWait"); 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; 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); this(file);
if (null != uploadPath){ if (null != uploadPath){
this.uploadPath = uploadPath; this.uploadPath = uploadPath;
this.uploadVirtualPath = null; this.uploadVirtualPath = uploadVirtualPath;
} }
} }
@ -84,13 +84,10 @@ public class BladeFile {
File file = new File(uploadPath); File file = new File(uploadPath);
if(null != fileFactory){ if(null != fileFactory){
this.uploadPath = fileFactory.path(file); String [] path = fileFactory.path(file);
if(Cst.me().isRemoteMode()){ this.uploadPath = path[0];
this.uploadVirtualPath = null; this.uploadVirtualPath = path[1].replace(Cst.me().getContextPath(), "");
} else{ file = fileFactory.rename(path[0], file);
this.uploadVirtualPath = fileFactory.virtualPath(file).replace(Cst.me().getContextPath(), "");
}
file = fileFactory.rename(file);
} }
File dir = file.getParentFile(); File dir = file.getParentFile();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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