Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

上传体验升级 #355

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion disconf-web/html/mainTpl/newconfig_file.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h2 class="autoPush-detail-title">新建配置文件</h2>
<span id="error_rar" class="text-error error_rar"></span>

<div style="color: #999;">
支持任意类型配置文件(.properties文件可支持自动注入,非.properties文件则只是简单托管)
支持任意类型配置文件(.properties文件可支持自动注入,.zip文件可以自动搜索其中的properties文件列表,非.properties文件则只是简单托管)
<span id="status_rar" class="text-success"></span>
</div>
<div class="progress_rar">
Expand Down
2 changes: 1 addition & 1 deletion disconf-web/html/newconfig_file.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ <h2 class="autoPush-detail-title">新建配置文件</h2>
<span id="error_rar" class="text-error error_rar"></span>

<div style="color: #999;">
支持任意类型配置文件(.properties文件可支持自动注入,非.properties文件则只是简单托管)
支持任意类型配置文件(.properties文件可支持自动注入,.zip文件可以自动搜索其中的properties文件列表,非.properties文件则只是简单托管)
<span id="status_rar" class="text-success"></span>
</div>
<div class="progress_rar">
Expand Down
7 changes: 6 additions & 1 deletion disconf-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>

<!-- build -->
Expand Down
149 changes: 149 additions & 0 deletions disconf-web/src/main/java/com/baidu/disconf/web/utils/ZipUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.baidu.disconf.web.utils;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;


/**
* 压缩或解压zip:
* 由于直接使用java.util.zip工具包下的类,会出现中文乱码问题,所以使用ant.jar中的org.apache.tools.zip下的工具类
* @author jack_lcz
*/

public class ZipUtil {
private static byte[] _byte = new byte[1024] ;
/**
* 压缩文件或路径
* @param zip 压缩的目的地址
* @param srcFiles 压缩的源文件
*/
public static void zipFile( String zip , List<File> srcFiles ){
try {
if( zip.endsWith(".zip") || zip.endsWith(".ZIP") ){
ZipOutputStream _zipOut = new ZipOutputStream(new FileOutputStream(new File(zip))) ;
_zipOut.setEncoding("GBK");
for( File _f : srcFiles ){
handlerFile(zip , _zipOut , _f , "");
}
_zipOut.close();
}else{
System.out.println("target file[" + zip + "] is not .zip type file");
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

/**
*
* @param zip 压缩的目的地址
* @param zipOut
* @param srcFile 被压缩的文件信息
* @param path 在zip中的相对路径
* @throws IOException
*/
private static void handlerFile(String zip , ZipOutputStream zipOut , File srcFile , String path ) throws IOException{
System.out.println(" begin to compression file[" + srcFile.getName() + "]");
if( !"".equals(path) && ! path.endsWith(File.separator)){
path += File.separator ;
}
if( ! srcFile.getPath().equals(zip) ){
if( srcFile.isDirectory() ){
File[] _files = srcFile.listFiles() ;
if( _files.length == 0 ){
zipOut.putNextEntry(new ZipEntry( path + srcFile.getName() + File.separator));
zipOut.closeEntry();
}else{
for( File _f : _files ){
handlerFile( zip ,zipOut , _f , path + srcFile.getName() );
}
}
}else{
InputStream _in = new FileInputStream(srcFile) ;
zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
int len = 0 ;
while( (len = _in.read(_byte)) > 0 ){
zipOut.write(_byte, 0, len);
}
_in.close();
zipOut.closeEntry();
}
}
}

/**
* 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下
*/
public static List<File> upzipFile(String zipPath, String descDir) {
return upzipFile( new File(zipPath) , descDir ) ;
}

/**
* 对.zip文件进行解压缩
* @param zipFile 解压缩文件
* @param descDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
* @return
*/
@SuppressWarnings("rawtypes")
public static List<File> upzipFile(File zipFile, String descDir) {
List<File> _list = new ArrayList<File>() ;
try {
ZipFile _zipFile = new ZipFile(zipFile , "GBK") ;
for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){
ZipEntry entry = (ZipEntry)entries.nextElement() ;
File _file = new File(descDir + File.separator + entry.getName()) ;
if( entry.isDirectory() ){
_file.mkdirs() ;
}else{
File _parent = _file.getParentFile() ;
if( !_parent.exists() ){
_parent.mkdirs() ;
}
InputStream _in = _zipFile.getInputStream(entry);
OutputStream _out = new FileOutputStream(_file) ;
int len = 0 ;
while( (len = _in.read(_byte)) > 0){
_out.write(_byte, 0, len);
}
_in.close();
_out.flush();
_out.close();
_list.add(_file) ;
}
}
} catch (IOException e) {
}
return _list ;
}

/**
* 对临时生成的文件夹和文件夹下的文件进行删除
*/
public static void deletefile(String delpath) {
try {
File file = new File(delpath);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + File.separator + filelist[i]);
if (!delfile.isDirectory()) {
delfile.delete();
} else if (delfile.isDirectory()) {
deletefile(delpath + File.separator + filelist[i]);
}
}
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.baidu.disconf.core.common.utils.FileUtils;
import com.baidu.disconf.web.utils.ZipUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,6 +27,11 @@
import com.baidu.dsp.common.exception.FileUploadException;
import com.baidu.dsp.common.vo.JsonObjectBase;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* 专用于配置新建
*
Expand All @@ -35,6 +43,7 @@
public class ConfigNewController extends BaseController {

protected static final Logger LOG = LoggerFactory.getLogger(ConfigUpdateController.class);
private static final String TMP_FILE_PATH = "/tmp";

@Autowired
private ConfigMgr configMgr;
Expand Down Expand Up @@ -75,44 +84,21 @@ public JsonObjectBase newItem(@Valid ConfNewItemForm confNewForm) {
*/
@ResponseBody
@RequestMapping(value = "/file", method = RequestMethod.POST)
public JsonObjectBase updateFile(@Valid ConfNewForm confNewForm, @RequestParam("myfilerar") MultipartFile file) {
public JsonObjectBase updateFile(@Valid ConfNewForm confNewForm, @RequestParam("myfilerar") MultipartFile file) throws IOException {

LOG.info(confNewForm.toString());

//
// 校验
//
int fileSize = 1024 * 1024 * 4;
String[] allowExtName = {".properties", ".xml"};
String[] allowExtName = {".properties", ".xml",".zip"};
fileUploadValidator.validateFile(file, fileSize, allowExtName);

//
// 更新
//
String fileContent = "";
try {

fileContent = new String(file.getBytes(), "UTF-8");
LOG.info("receive file: " + fileContent);

} catch (Exception e) {
// 处理(新增压缩包)
String retMessage = handleAndSaveFileContent(confNewForm,file);

LOG.error(e.toString());
throw new FileUploadException("upload file error", e);
}

// 创建配置文件表格
ConfNewItemForm confNewItemForm = new ConfNewItemForm(confNewForm);
confNewItemForm.setKey(file.getOriginalFilename());
confNewItemForm.setValue(fileContent);

// 业务校验
configValidator.validateNew(confNewItemForm, DisConfigTypeEnum.FILE);

//
configMgr.newConfig(confNewItemForm, DisConfigTypeEnum.FILE);

return buildSuccess("创建成功");
return buildSuccess(retMessage);
}

/**
Expand Down Expand Up @@ -144,4 +130,62 @@ public JsonObjectBase updateFileWithText(@Valid ConfNewForm confNewForm, @NotNul

return buildSuccess("创建成功");
}


/**
* 配置文件,统一处理方法
*/
private String handleAndSaveFileContent(ConfNewForm confNewForm, MultipartFile file) throws IOException {
//step 1: 获取文件后缀信息
String filename = file.getOriginalFilename();
String extName = filename.substring(filename.lastIndexOf(".")).toLowerCase();

//step 2: 定义待处理的文件列表数据
List<File> configFiles = new ArrayList<File>();

//step 3: 处理文件列表
File tmpConfigFile = new File(TMP_FILE_PATH,file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), tmpConfigFile);
if(StringUtils.equals(extName,".properties")){
configFiles.add(tmpConfigFile);
}else if(StringUtils.equals(extName,".zip")){
configFiles = ZipUtil.upzipFile(tmpConfigFile,TMP_FILE_PATH);
}else{
//Nothing
}

//step 4: 保存文件信息
saveFileContent(configFiles,confNewForm);

return "处理成功";

}

private void saveFileContent(List<File> configFiles,ConfNewForm confNewForm){

for(File file : configFiles){
String fileContent = "";
try {
fileContent = new String(FileUtils.readFileToString(file,"UTF-8"));
LOG.info("receive file: " + fileContent);

} catch (Exception e) {
LOG.error(e.toString());
throw new FileUploadException("upload file error", e);
}


// 创建配置文件表格
ConfNewItemForm confNewItemForm = new ConfNewItemForm(confNewForm);
confNewItemForm.setKey(file.getName());
confNewItemForm.setValue(fileContent);

// 业务校验
configValidator.validateNew(confNewItemForm, DisConfigTypeEnum.FILE);

// 目前都是没有事务支持的,@jack_lcz
configMgr.newConfig(confNewItemForm, DisConfigTypeEnum.FILE);
}
}

}