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 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
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(); } } }
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.
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!