在Java中实现附件链接下载及上传功能

在我们的日常开发中,经常会遇到需要处理文件上传、下载的情况。本文将介绍如何在Java中实现附件链接下载后上传附件的功能。

下载附件链接

首先,我们需要实现一个功能,能够下载指定链接的附件文件。我们可以使用Java中的HttpURLConnection来实现这一功能。以下是一个示例代码:

import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;

public class FileDownload {

    public static void downloadFile(String fileURL, String saveDir) throws IOException {
        URL url = new URL(fileURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        int responseCode = httpConn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = "";
            String disposition = httpConn.getHeaderField("Content-Disposition");
            String contentType = httpConn.getContentType();
            int contentLength = httpConn.getContentLength();

            if (disposition != null) {
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10, disposition.length() - 1);
                }
            } else {
                fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1);
            }

            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = saveDir + File.separator + fileName;

            FileOutputStream outputStream = new FileOutputStream(saveFilePath);

            int bytesRead;
            byte[] buffer = new byte[4096];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();
            System.out.println("File downloaded: " + saveFilePath);
        } else {
            System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        }
        httpConn.disconnect();
    }

    public static void main(String[] args) throws IOException {
        String fileURL = "
        String saveDir = "C:/downloads";
        downloadFile(fileURL, saveDir);
    }
}

上传附件

接着,我们需要实现一个上传附件的功能。我们可以使用Apache的Commons FileUpload来实现这一功能。以下是一个示例代码:

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

public class FileUpload {

    public void uploadFile(HttpServletRequest request, String uploadDir) {
        if (ServletFileUpload.isMultipartContent(request)) {
            try {
                List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

                for (FileItem item : multiparts) {
                    if (!item.isFormField()) {
                        String name = new File(item.getName()).getName();
                        item.write(new File(uploadDir + File.separator + name));
                    }
                }

                System.out.println("File uploaded successfully");
            } catch (Exception e) {
                System.out.println("File upload failed");
            }
        }
    }
}

类图

下面是下载和上传附件的类图:

classDiagram
    class FileDownload {
        - downloadFile(String fileURL, String saveDir)
        - main(String[] args)
    }

    class FileUpload {
        - uploadFile(HttpServletRequest request, String uploadDir)
    }

序列图

下面是下载和上传附件的序列图:

sequenceDiagram
    participant Client
    participant Server
    Client ->> Server: 请求下载文件
    Server ->> Server: 下载文件
    Server ->> Client: 返回文件
    Client ->> Server: 上传文件
    Server ->> Server: 保存文件
    Server ->> Client: 返回上传成功

通过以上代码示例和图示,我们可以在Java中实现附件链接下载后上传附件的功能。希望本文对你有所帮助!