Jersey 简介


Jersey 是开源的 RESTful 框架,实现了 JAX-RS 标准,提供了更多的个性和工具, 能够进一步地简化 RESTful serviceclient 开发,与 Struts 相似,它同样能够和 HibernateSpring 框架整合。此处应用它实现文件上传性能。

引入依赖


pom.xml 中增加 Jersey 相干依赖

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

而后创立一个名为 FileUtils 的文件工具类。

生成临时文件办法


/**   * 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;}

加密/解密文件办法


// 加密/解密文件的密钥public static final int CRYPTO_SECRET_KEY = 0x99;/**   * 加密/解密文件   * @param srcFile 原文件   * @param encFile 加密/解密后的文件   * @return 加密/解密后的文件   * @throws Exception*/public static File 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();    return encFile;}

上传文件办法


/**   * 上传文件   * @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(".")));            // 执行加密并笼罩原文件            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;}

残缺工具类


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("/") + "\\static\\temp");    // 上传文件并返回文件门路    String uploadFilePath = FileUtil.uploadByJersey("http://localhost:8080", "/upload", tempFile, isCrypto);    if (uploadFilePath != null) {        return "上传胜利";    }    else {        return "上传失败";    }}
更多干货请移步:https://antoniopeng.com
  • 文章作者:彭超
  • 本文首发于集体博客:https://antoniopeng.com
  • 版权申明:本博客所有文章除特地申明外,均采纳 CC BY-NC-SA 4.0 许可协定。转载请注明来自 彭超的博客!