永中文档DCS在线转换文件(代码示例)

来源:互联网 发布:best video 软件 编辑:程序博客网 时间:2024/05/29 09:00
package test;

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;

public class Test {
public static void main(String[] args) {
File file=new File("");
String convertByFile = SubmitPost("http://dcs.zwtd.cn/dcs.web/upload",
"E:/新建.pptx", "1");
//  String convertByUrl =
//  sendPost("http://dcs.zwtd.cn/dcs.web/onlinefile" ,
//  "downloadUrl=");
System.out.println(convertByFile);
//  System.out.println(convertByUrl);
}


public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new 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;SV1)");
conn.setDoInput(true);
conn.setDoOutput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
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 {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;

}

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); 
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();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// requestJson = e.toString();
} finally {
try {
httpclient.getConnectionManager().shutdown();
} catch (Exception ignore) {

}
}
// requestJson =(requestJson.substring(requestJson.indexOf("[\"")+1,requestJson.indexOf("\"]"))).substring(1,requestJson.substring(requestJson.indexOf("[\"")+1,requestJson.indexOf("\"]")).length());
return requestJson;
}

}