package com.example.fuzzControll.tools; import com.example.fuzzControll.conf.AflnetProperties; import com.example.fuzzControll.conf.SpringContextUtil; import com.example.fuzzControll.exception.FileException; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; @Slf4j public class FileTools { AflnetProperties properties = (AflnetProperties) SpringContextUtil.getBean("AflnetProperties"); public void load(MultipartFile file) throws FileException { 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 !"); } } /** * 文件备份至数据库 * * @param filename * @return */ public byte[] fileReadAndTranstoBytes(String path, String filename) { File file = new File(path + filename); byte[] buffer = new byte[(int) file.length()]; try ( FileInputStream inputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);) { System.out.println("1-2"); if (file.length() > Integer.MAX_VALUE && file.length() <= 0) { log.error("KittyLogFile is too long or has no content!"); } System.out.println("1-3"); int bytesRead; while ((bytesRead = bufferedInputStream.read(buffer)) != -1) { System.out.println(buffer.length); } System.out.println("1-5"); } catch (Exception e) { e.printStackTrace(); } return buffer; } /** * 读取数据库文件至本地 */ public void loadFileInLocal(String fileLoadPath, String fileName, byte[] fileBytes) { File file = new File(fileLoadPath + "/" + fileName); try (FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBytes); BufferedInputStream bufferedInputStream = new BufferedInputStream(byteArrayInputStream);) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = bufferedInputStream.read(buffer)) != -1) { bufferedOutputStream.write(buffer, 0, bytesRead); } } catch (FileNotFoundException e) { throw new FileException("Cannot find path:" + fileLoadPath + "! Maybe there is no permission for this file!"); } catch (IOException e) { throw new FileException("Load file failed!"); } } }