Commit 0745a33d by 钱炳权

test

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