国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java上傳下載文件并實現加密解密

瀏覽:72日期:2022-09-02 18:52:02

使用 Jersey 服務器實現上傳,使用 HTTP 請求實現下載

引入依賴

在 pom.xml 中添加 Jersey 相關依賴

<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18.1</version></dependency>

創建工具類

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; /** * 加密/解密 文件 * @param srcFile 原文件 * @param encFile 加密/解密后的文件 * @throws Exception */ 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(); } /** * MultipartFile 生成臨時文件 * @param multipartFile * @param tempFilePath 臨時文件路徑 * @return File 臨時文件 */ 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; } /** * 上傳文件 * @param fileServerPath文件服務器地址 * @param folderPath 存放的文件夾路徑(比如存放在文件服務器的 upload 文件夾下,即 ”/upload“) * @param uploadFile需要上傳的文件 * @param isCrypto是否加密 * @return String文件上傳后的全路徑 */ 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; } // 創建 Jersey 服務器 Client client = Client.create(); WebResource wr = client.resource(fullPath); // 上傳文件 wr.put(String.class, fileToByte(uploadFile)); } catch (Exception e) { e.printStackTrace(); } return fullPath; } /** * 下載文件 * @param url 文件路徑 * @param filePath 文件保存路徑 * @param fileName文件名稱(包含文件后綴) * @param isCrypto 是否解密 * @return File */ 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; } /** * 刪除文件服務器上的文件 * @param url 文件路徑 * @return boolean */ 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; } /** * File轉Bytes * @param file * @return byte[] */ 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; }}

測試上傳

/** * @param multipartFile 上傳文件 * @param isCrypto 是否加密文件 * @return */@Testpublic String upload(MultipartFile multipartFile, boolean isCrypto) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); // 生成臨時文件 File tempFile = FileUtil.multipartFileToFile(multipartFile, request.getServletContext().getRealPath('/') + 'statictemp'); // 上傳文件并返回文件路徑 String uploadFilePath = FileUtil.uploadByJersey('http://localhost:8080', '/upload', tempFile, isCrypto); if (uploadFilePath != null) { return '上傳成功'; } else { return '上傳失敗'; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 中文 日本 免费 高清 | 国产美女做爰免费视 | 男女朋友做爽爽爽免费视频网 | 99久久综合精品国产 | 亚洲午夜久久久久国产 | 黄 色 成 年 人小说 | 久久黄色精品视频 | 91精品国产手机 | 国产一区二区三区在线观看影院 | 国产成人自拍在线 | 日本一线一区二区三区免费视频 | 欧美一区二区三区免费高 | 一区二区三区精品国产欧美 | 97国产在线播放 | 最全精品自拍视频在线 | 久久精品免费视频观看 | 中国人免费观看高清在线观看二区 | 欧美一级网站 | 欧美les视频xxxx在线观看 | 亚洲欧美一区二区三区在线观看 | 精品视频一区在线观看 | 韩国一级特黄毛片大 | 一级特级aaaa毛片免费观看 | 亚洲在线偷拍自拍 | 日本成本人片 | 欧美在线视频不卡 | 国产亚洲欧美在线视频 | 高清成人爽a毛片免费网站 高清大学生毛片一级 | 在线视频免费国产成人 | 午夜精品亚洲 | 一级aaaaa毛片免费视频 | 亚洲美女在线播放 | 国产精品免费大片一区二区 | 国产日韩精品欧美一区 | 国外免费一级 | 国产99久久九九精品免费 | 欧美人成在线观看 | 午夜啪啪福利视频 | 亚洲悠悠色综合中文字幕 | 亚洲成人影院在线观看 | 国产在视频线精品视频二代 |