Commit 0745a33d by 钱炳权

test

parent c96a10bd
...@@ -34,7 +34,6 @@ public class KittyServerMessageController { ...@@ -34,7 +34,6 @@ public class KittyServerMessageController {
/** /**
* 获取服务器templateInfo信息 * 获取服务器templateInfo信息
*/ */
//todo first
@RequestMapping(value = "/templateInfo", method = RequestMethod.GET) @RequestMapping(value = "/templateInfo", method = RequestMethod.GET)
public AjaxResult getTemplateInfo( ) { public AjaxResult getTemplateInfo( ) {
try { try {
......
...@@ -15,6 +15,9 @@ public class BaseException extends RuntimeException{ ...@@ -15,6 +15,9 @@ public class BaseException extends RuntimeException{
*/ */
private String module; private String module;
public BaseException() {
}
public BaseException(String defaultMessage, String module) { public BaseException(String defaultMessage, String module) {
this.defaultMessage = defaultMessage; this.defaultMessage = defaultMessage;
this.module = module; this.module = module;
......
...@@ -3,8 +3,12 @@ package com.example.fuzzControll.exception; ...@@ -3,8 +3,12 @@ package com.example.fuzzControll.exception;
/** /**
* cmd运行异常 * cmd运行异常
*/ */
public class CmdException extends BaseException{ public class CmdException extends BaseException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public CmdException() {
}
public CmdException(String defaultMessage) { public CmdException(String defaultMessage) {
super(defaultMessage, "cmd"); super(defaultMessage, "cmd");
} }
......
...@@ -10,5 +10,5 @@ public interface SeedFileService { ...@@ -10,5 +10,5 @@ public interface SeedFileService {
void delFile(String fileName); void delFile(String fileName);
void upload(MultipartFile file); void upload(MultipartFile file);
int getSeedFileCount(); int getSeedFileCount(String msg);
} }
...@@ -23,27 +23,42 @@ public class SeedFileServiceImpl implements SeedFileService { ...@@ -23,27 +23,42 @@ public class SeedFileServiceImpl implements SeedFileService {
@Override @Override
public List<String> getSeedFiles() throws CmdException{ public List<String> getSeedFiles() throws CmdException{
return cmdTools.runCmd(CmdConstent.GET_FILE_NAME + properties.getSeedPath()); return cmdTools.runCmd(CmdConstent.GET_FILE_NAME + properties.getSeedPath(),"getSeedFiles");
} }
//todo 同步修改可能会出现问题 //todo 同步修改可能会出现问题
@Override @Override
public void delFile(String fileName) throws CmdException { public void delFile(String fileName) throws CmdException {
int fileCountBefore = getSeedFileCount(); int fileCountBefore = 0;
cmdTools.runCmd(CmdConstent.DELETE_FILE + properties.getSeedPath() + "/" + fileName); int fileCountAfter = 0;
int fileCountAfter = getSeedFileCount(); try {
fileCountBefore = getSeedFileCount("delFile before.");
cmdTools.runCmd(CmdConstent.DELETE_FILE + properties.getSeedPath() + "/" + fileName,"delFile");
fileCountAfter = getSeedFileCount("delFile after.");
} catch (CmdException e) {
throw new CmdException(e.getMessage());
}
if(fileCountAfter==fileCountBefore){ if(fileCountAfter==fileCountBefore){
throw new CmdException("delete cmd error !The file has not changed."); throw new CmdException("Delete unsuccess ! The file has not changed .Attempt to change permissions.");
} }
} }
@Override @Override
public void upload(MultipartFile file) throws FileException { public void upload(MultipartFile file) throws FileException,CmdException {
int fileCountBefore = getSeedFileCount();
fileTools.load(file); int fileCountBefore = 0;
int fileCountAfter = getSeedFileCount(); int fileCountAfter = 0;
try {
fileCountBefore = getSeedFileCount("upload before.");
fileTools.load(file);
fileCountAfter = getSeedFileCount("upload after.");
} catch (CmdException e) {
throw new CmdException(e.getMessage());
} catch (FileException e){
throw new FileException(e.getMessage());
}
if(fileCountAfter==fileCountBefore){ if(fileCountAfter==fileCountBefore){
throw new CmdException("upload file error !The file failed to be submitted."); throw new FileException("upload file error !The file failed to be submitted.Attempt to change permissions.");
} }
} }
...@@ -52,9 +67,14 @@ public class SeedFileServiceImpl implements SeedFileService { ...@@ -52,9 +67,14 @@ public class SeedFileServiceImpl implements SeedFileService {
* 获取种子文件目录下文件数量 * 获取种子文件目录下文件数量
*/ */
@Override @Override
public int getSeedFileCount() throws CmdException { public int getSeedFileCount(String msg) throws CmdException {
List<String> files = cmdTools.runCmd(CmdConstent.GET_FILE_NAME+ properties.getSeedPath()); int count = 0;
int count = files.size(); try {
List<String> files = cmdTools.runCmd(CmdConstent.GET_FILE_NAME+ properties.getSeedPath(),"getSeedFileCount");
count = files.size();
} catch (CmdException e) {
throw new CmdException(e.getMessage()+" when "+msg);
}
return count; return count;
} }
} }
...@@ -24,7 +24,7 @@ public class CmdTools { ...@@ -24,7 +24,7 @@ public class CmdTools {
/** /**
* 运行不需要后台运行cmd * 运行不需要后台运行cmd
*/ */
public List<String> runCmd(String cmd) throws CmdException { public List<String> runCmd(String cmd,String caller) throws CmdException {
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
try { try {
Process process = Runtime.getRuntime().exec(cmd); Process process = Runtime.getRuntime().exec(cmd);
...@@ -32,7 +32,7 @@ public class CmdTools { ...@@ -32,7 +32,7 @@ public class CmdTools {
printMessage(process.getErrorStream(), new ArrayList<String>()); printMessage(process.getErrorStream(), new ArrayList<String>());
process.waitFor(); process.waitFor();
} catch (Exception e) { } catch (Exception e) {
throw new CmdException("run delete or search cmd error !"); throw new CmdException(caller+" run cmd failed!");
} }
return result; return result;
......
...@@ -3,6 +3,7 @@ package com.example.fuzzControll.tools; ...@@ -3,6 +3,7 @@ package com.example.fuzzControll.tools;
import com.example.fuzzControll.conf.SpringContextUtil; import com.example.fuzzControll.conf.SpringContextUtil;
import com.example.fuzzControll.conf.SeedProperties; import com.example.fuzzControll.conf.SeedProperties;
import com.example.fuzzControll.exception.FileException; import com.example.fuzzControll.exception.FileException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
...@@ -10,24 +11,25 @@ import java.io.BufferedOutputStream; ...@@ -10,24 +11,25 @@ import java.io.BufferedOutputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
@Slf4j
public class FileTools { public class FileTools {
SeedProperties properties = (SeedProperties) SpringContextUtil.getBean("seedProperties"); SeedProperties properties = (SeedProperties) SpringContextUtil.getBean("seedProperties");
public void load(MultipartFile file) throws FileException { public void load(MultipartFile file) throws FileException {
if (file==null){ if (file == null) {
throw new FileException("upload file is null !"); throw new FileException("upload file is null !");
} }
try (InputStream inputStream = file.getInputStream(); try (InputStream inputStream = file.getInputStream();
FileOutputStream outputStream = new FileOutputStream(properties.getSeedPath() + "/" + file.getOriginalFilename());) { FileOutputStream outputStream = new FileOutputStream(properties.getSeedPath() + "/" + file.getOriginalFilename());) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int bytesRead; int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) { while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead); bufferedOutputStream.write(buffer, 0, bytesRead);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new FileException("write file error !"); throw new FileException("write file error !");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment