1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientHandlerException; import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.api.client.WebResource; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.UUID;
public class FileUtils {
public static final int CRYPTO_SECRET_KEY = 0x99;
public static int FILE_DATA = 0;
public static void cryptoFile(File srcFile, File encFile) throws Exception {
InputStream inputStream = new FileInputStream(srcFile); OutputStream outputStream = new FileOutputStream(encFile); while ((FILE_DATA = inputStream.read()) > -1) { outputStream.write(FILE_DATA ^ CRYPTO_SECRET_KEY); } inputStream.close(); outputStream.flush(); outputStream.close(); }
public static File multipartFileToFile(MultipartFile multipartFile, String tempFilePath) {
File file = new File(tempFilePath); String originalFilename = multipartFile.getOriginalFilename(); String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")); if (!file.exists()) { file.mkdirs(); } File tempFile = new File(tempFilePath + "\\" + UUID.randomUUID().toString().replaceAll("-", "") + suffix); try { if (!tempFile.exists()) { multipartFile.transferTo(tempFile); } } catch (IOException e) { e.printStackTrace(); } return tempFile; }
public static String uploadByJersey(String fileServerPath, String folderPath, File uploadFile, boolean isCrypto) {
String suffix = uploadFile.getName().substring(uploadFile.getName().lastIndexOf(".")); String randomFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix; String fullPath = fileServerPath + folderPath + "/" + randomFileName; try { if (isCrypto) { File cryptoFile = new File(uploadFile.getPath().substring(0, uploadFile.getPath().lastIndexOf(".")) + "crypto" + uploadFile.getPath().substring(uploadFile.getPath().lastIndexOf("."))); cryptoFile(uploadFile, cryptoFile); uploadFile = cryptoFile; } Client client = Client.create(); WebResource wr = client.resource(fullPath); wr.put(String.class, fileToByte(uploadFile)); } catch (Exception e) { e.printStackTrace(); } return fullPath; }
public static File downloadByURL(String url, String filePath, String fileName, boolean isCrypto) {
File file = new File(filePath); if (!file.exists()) { file.mkdirs(); } FileOutputStream fileOut; HttpURLConnection httpURLConnection; InputStream inputStream; try { URL httpUrl = new URL(url); httpURLConnection = (HttpURLConnection) httpUrl.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); httpURLConnection.connect(); inputStream = httpURLConnection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); if (!filePath.endsWith("\\")) { filePath += "\\"; } file = new File(filePath + fileName); fileOut = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOut); byte[] bytes = new byte[4096]; int length = bufferedInputStream.read(bytes); while (length != -1) { bufferedOutputStream.write(bytes, 0, length); length = bufferedInputStream.read(bytes); } bufferedOutputStream.close(); bufferedInputStream.close(); httpURLConnection.disconnect(); } catch (Exception e) { e.printStackTrace(); } if (isCrypto) { try { File cryptoFile = new File(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServletContext().getRealPath("/") + "\\temp\\" + UUID.randomUUID().toString().replaceAll("-", "") + file.getName().substring(file.getName().lastIndexOf("."))); cryptoFile(file, cryptoFile); file.delete(); file = cryptoFile; } catch (Exception e) { e.printStackTrace(); } } return file; }
public static boolean deleteByJersey(String url) {
try { Client client = new Client(); WebResource webResource = client.resource(url); webResource.delete(); return true; } catch (UniformInterfaceException e) { e.printStackTrace(); } catch (ClientHandlerException e) { e.printStackTrace(); } return false; }
public static byte[] fileToByte(File file) {
byte[] buffer = null; try { FileInputStream fileInputStream = new FileInputStream(file); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int n; while ((n = fileInputStream.read(bytes)) != -1) { byteArrayOutputStream.write(bytes, 0, n); } fileInputStream.close(); byteArrayOutputStream.close(); buffer = byteArrayOutputStream.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } }
|