如果需要直接運行,請直接修改代碼中的兩個參數
轉換類型在下方,根據輸入以及輸出類型選擇
String url = "文件地址";
String convertType = "轉換類型";
代碼實現講解
其中包括main方法可以直接實現
其中的方法是靜態的,可以作為工具類
首先定義文件url和轉換類型(下方有轉換類型)
String url = “文件地址”;
String convertType = “轉換類型”;
// 文件上傳轉換
String convertByFile = SubmitPost("http://dcs.yozosoft.com:80/upload", url,convertType);
String convertByUrl = SubmitPost方法有三個參數(第一個無需更改,第二個是需要在線預覽的文件地址,第三個是文件轉換的類型)
// 網絡地址轉換
sendPost("http://dcs.yozosoft.com:80/onlinefile", "downloadUrl=http://img.
iyocloud.com:8000/+url+&convertType=" + convertType);
sendPost方法有二個參數(第一個無需更改,第二個是需要在線預覽的文件地址以及文件轉換的類型)
SubmitPost會返回一個類似
{“result”:0,“data”:[“http://dcs.yozosoft.com/view/2021/04/07/MjEwNDA3ODQ1MjYzNzIx.html”],“message”:“轉換成功”,“type”:0}
的字符串
通過indexOf()方法獲取最終需要的URL的位置,
再使用subString()方法獲取出最終URL
則可以使用該URL在瀏覽器訪問
int start = convertByFile.indexOf("http://");
int stop = convertByFile.indexOf(".html");
String URL = convertByFile.substring(start,stop + 5);
文件轉換類型如下
0-----文檔格式到高清html的轉換
1-----文檔格式到html的轉換
2-----文檔格式到txt的轉換
3-----文檔格式到pdf的轉換
4-----文檔格式到gif的轉換
5-----文檔格式到png的轉換
6-----文檔格式到jpg的轉換
7-----文檔格式到tiff的轉換
8-----文檔格式到bmp的轉換
9-----pdf文檔格式到gif的轉換
10----pdf文檔格式到png的轉換
11----pdf文檔格式到jpg的轉換
12----pdf格式文檔到tiff的轉換
13----pdf文檔格式到bmp的轉換
14----pdf文檔格式到html的轉換
15----html文檔格式到微軟文檔格式的轉換
16----文檔轉換多個SVG返回分頁加載頁面(模版)
17----tif文件轉成html
18----文檔轉換多個SVG
19----壓縮文件到html的轉換(模版)
20----PDF文件到html的轉換(模版)
21----ofd文件到html的轉換(模版)
22----兩個doc文檔合并
23----圖片到html的轉換
24----pdf文檔格式到txt的轉換
25----文檔按頁轉換(高清版)
26----文檔按頁轉換(標準版)
27----獲取文檔頁碼(MS文檔)
28----獲取pdf頁碼(PDF文件)
29----文檔到ofd的轉換
30----文檔到html(圖片)的轉換
31----多個pdf文檔合并
32----圖片到pdf的轉換
33----文檔到文檔的轉換
34----pdf到pdf的轉換
35----tif到html的轉換(模板)
具體代碼實現
需要用到的第三方工具包為:commons-logging-1.1.jar,httpclient-4.5.jar,httpcore-4.4.1.jar,httpmime-4.5.jar,
如代碼出現編譯不過,請加入這四個包。
推薦一個jar包下載地址
推薦jar包下載地址
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* @Description: DCS文檔轉換服務Java調用代碼示例
* @author LB
* @date 20151014
*/
public class DCSTest {
/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("Accept-Charset", "UTF-8");
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 )compatible; MSIE 6.0; Windows NT 5.1;SV
1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
{ catch (Exception e) }
System.out.println("發送 POST 請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally模塊來關閉輸出流、輸入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
{ catch (IOException ex) }
ex.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 上傳文件POST方法的請求
*
* @param url 發送請求的 URL
* @param filepath 文件路徑
* @param type 轉換類型
* @return 所代表遠程資源的響應結果, json數據
*/
public static String SubmitPost(String url, String filepath, String type) {
String requestJson = "";
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(url);
FileBody file = new FileBody(new File(filepath));
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
Charset.forName("UTF-8"));
reqEntity.addPart("file", file);// file為后臺請求的File upload屬性;
reqEntity.addPart("convertType", new StringBody(type, Charset.forName("UTF-8")));
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus·SC_OK) {
HttpEntity resEntity = response.getEntity();
requestJson = EntityUtils.toString(resEntity);
EntityUtils.consume(resEntity);
}
{ catch (ParseException e) }
// TODO Auto-generated catch block
e.printStackTrace();
// requestJson = e.toString();
{ catch (IOException e) }
// TODO Auto-generated catch block
e.printStackTrace();
// requestJson = e.toString();
{ finally }
try {
httpclient.getConnectionManager().shutdown();
{ catch (Exception ignore) }
}
}
return requestJson;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//首先定義文件url和轉換類型(上方有轉換類型)
String url = "文件地址";
//示例
//C:/Users/admin/Desktop/examp.word
String convertType = "轉換類型";
//轉換類型在雙引號中填寫轉換類型對應數字
// 文件上傳轉換
String convertByFile = SubmitPost("http://dcs.yozosoft.com:80/upload", url,convertType);
//String convertByUrl = SubmitPost方法有三個參數(第一個無需更改,第二個是需要在線預覽的文件地址,第三個是文件轉換的類型)
// 網絡地址轉換
String convertByUrl = WordUtil.sendPost("http://dcs.yozosoft.com:80/onlinefile",
"downloadUrl=http://img.iyocloud.com:8000/"+ url +"&convertType=" + convertType);
//sendPost方法有二個參數(第一個無需更改,第二個是需要在線預覽的文件地址以及文件轉換的類型)
int start = convertByFile.indexOf("http://");
int stop = convertByFile.indexOf(".html");
url = convertByFile.substring(start,stop+5);
System.out.println(url);
}
}
————————————————
版權聲明:本文為CSDN博主「qq_562790526」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_45998799/article/details/115482620
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.