import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class pdf2word
{
final private static String GET_PREFIX = "http://online2pdf.com/progress_frame?upload_id=";
final private static String POST_PREFIX = "http://online2pdf.com/conversion/frame";
private static UUID upload_guid;
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private static void sendHTTPPostRequest()
{
Map<String,Object> params = new LinkedHashMap<>();
params.put("UPLOAD_ID", upload_guid);
params.put("MAX_FILE_SIZE", "52428800");
params.put("file_order", "0_0;1_0;");
params.put("userfile[0][]", "File Name=\"1.pdf\"");
params.put("conversion_mode", "merge");
params.put("convert", "1");
params.put("orientation", "auto");
params.put("merge_mode", "serial");
params.put("output_format", "pdf");
params.put("page_size", "a4");
params.put("quality", "80");
try {
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
URL url = null;
try {
url = new URL("http://online2pdf.com/conversion/frame");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0; System.out.print((char)c));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void sendHTTPGetRequest()
{
upload_guid = java.util.UUID.randomUUID();
String getURL = GET_PREFIX + upload_guid;
HttpGet httpgets = new HttpGet(getURL);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(httpgets);
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instreams = entity.getContent();
String str = convertStreamToString(instreams);
System.out.println(str);
}
} catch ( IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ClientProtocolException, IOException
{
sendHTTPGetRequest();
sendHTTPPostRequest();
}
}
转载:https://blog.csdn.net/i042416/article/details/101169239
查看评论