Home > Java > javaTutorial > Java development tips: How to call Qiniu Cloud document conversion interface to achieve format conversion

Java development tips: How to call Qiniu Cloud document conversion interface to achieve format conversion

PHPz
Release: 2023-07-05 20:49:17
Original
1836 people have browsed it

Java development tips: How to call Qiniu Cloud document conversion interface to implement format conversion

Introduction: In actual development work, we often encounter the need to convert a file from one format to another A format is required. For example, convert a Word document to PDF format, convert an Excel table to CSV format, etc. Qiniu Cloud provides a document conversion interface that allows us to easily implement these format conversion functions. In this article, I will use Java language as an example to introduce how to call Qiniu Cloud's document conversion interface to implement format conversion.

1. Preparation

  1. Register Qiniu Cloud account and obtain Access Key and Secret Key.
  2. Create a storage space in the Qiniu Cloud console and get the name of the storage space.
  3. Activate the document processing function in Qiniu Cloud Console and obtain the service URL of this function.

2. Code Example

The following code example shows how to use Java to call Qiniu Cloud’s document conversion interface to implement format conversion. In the code example, we take converting a Word document to PDF format as an example.

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.QiniuException;
import com.qiniu.processing.OperationManager;
import com.qiniu.processing.Pfop;
import com.qiniu.processing.OperationStatus;
import com.qiniu.processing.OperationStatusV2;

public class QiniuDocumentConverter {

    private static final String ACCESS_KEY = "your_access_key";
    private static final String SECRET_KEY = "your_secret_key";
    private static final String BUCKET_NAME = "your_bucket_name";
    private static final String DOC_CONVERT_SERVICE_URL = "http://api.qiniu.com/pfop/";

    public static void main(String[] args) {
        String localFilePath = "path_to_your_word_file.docx";
        String key = "converted_pdf_file.pdf";
        String pipeline = "your_pipeline";
        convertDocument(localFilePath, key, pipeline);
    }

    public static void convertDocument(String localFilePath, String key, String pipeline) {
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        StringMap params = new StringMap();
        params.putNotEmpty("bucket", BUCKET_NAME);
        params.putNotEmpty("key", key);
        params.putNotEmpty("fops", "docx2pdf");
        params.putNotEmpty("notifyURL", "your_notify_url");
        params.putNotEmpty("force", "true");
        params.putNotEmpty("pipeline", pipeline);

        String token = auth.uploadToken(BUCKET_NAME, null, 3600, params);
        UploadManager uploadManager = new UploadManager();
        try {
            Response response = uploadManager.put(localFilePath, key, token);
            String persistentId = response.jsonToMap().get("persistentId").toString();
            OperationManager operationManager = new OperationManager(auth);
            Pfop pfop = new Pfop(BUCKET_NAME, key, "docx2pdf", params);
            String id = operationManager.pfop(pfop);
            OperationStatus status = operationManager.prefop(id);
            System.out.println(status);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the code example, you need to replace your_access_key with your Qiniu Cloud Access Key, your_secret_key with your Qiniu Cloud Secret Key, ## Replace #your_bucket_name with the name of your storage space, path_to_your_word_file.docx with the path to your local Word file, and converted_pdf_file.pdf with the name of the PDF file you wish to convert. Key name, replace your_pipeline with the name of your transformation pipeline.

3. Summary

By calling the Qiniu Cloud document conversion interface, we can easily convert files in different formats. The above code example only shows how to convert a Word document to PDF format. In fact, Qiniu Cloud also supports more format conversions. For details, please refer to Qiniu Cloud's official documentation.

When using the Qiniu Cloud document conversion interface, you need to pay attention to setting appropriate conversion parameters (such as the conversion target format, conversion pipeline, etc.), and configure the corresponding parameters according to the document requirements. In addition, you also need to correctly set the Access Key and Secret Key of Qiniu Cloud, and specify the correct storage space and service URL.

I hope this article can help everyone make better use of Qiniu Cloud's document conversion function and improve development efficiency. If you have any questions or issues, please feel free to leave a message.

The above is the detailed content of Java development tips: How to call Qiniu Cloud document conversion interface to achieve format conversion. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template