赞
踩
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sinosoft.splatform.persistent.model.gwlzxt.*;
import com.sinosoft.splatform.service.gwlzxt.*;
import com.sinosoft.splatform.util.PostMenHu;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
// 从本地读取文件,然后返回文件流 这么做的目的是模仿调用 返回流的接口, 可以调用本地文件来实现 调用第二个方法就行 ----------------------
@ResponseBody
@RequestMapping("/getUrlFile")
public void getUrlFile(String url, HttpServletRequest request, HttpServletResponse response) {
String serverUrl = "C:/\\Users/\\15733/\\Desktop/\\28045111.pdf";
File file = new File(serverUrl);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
int length = inputStream.read(data);
inputStream.close();
response.setContentType("text/plain; charset=utf-8");
OutputStream stream = response.getOutputStream();
stream.write(data);
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回的类型可以按照自己的功能封装
public MultipartFile getIo(String title) {
MultipartFile multipartFile;
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try{
URL url = new URL("http://localhost:8085/ProPushDocumentController/getUrlFile");
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/json; charset="+"utf-8");
urlConnection.connect();
inputStream = urlConnection.getInputStream();
multipartFile = new MockMultipartFile(title,title,"", inputStream);
return multipartFile;
}catch (Exception e){
System.out.println("getFile error: " + e);
}finally {
try {
inputStream.close();
urlConnection.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
----------------------------结束 -------------------
----------------------------开始 --------------------
// 从本地读取文件,返回json格式 这么做的目的是模仿调用 返回json的接口, 可以调用本地文件来实现
@ResponseBody
@RequestMapping("/getJsonApi")
public com.alibaba.fastjson.JSONObject getJsonApi() throws Exception {
String jsonStr = "";
try {
File jsonFile = new File("C:/\\Users/\\15733/\\Desktop/\\111.txt");
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
com.alibaba.fastjson.JSONObject jobj = JSON.parseObject(jsonStr);
return jobj;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public com.alibaba.fastjson.JSONObject apiStream() {
CloseableHttpClient hc=HttpClients.createDefault();
//一个get请求对象,包装的请求地址,相当于浏览器中输入了网址
HttpPost get=new HttpPost("http://localhost:8085/ProPushDocumentController/getJsonApi");
//当客户端关闭请求的时候,通知服务端也关闭连接。
get.setHeader("Connection","close");
HttpResponse resp=null;
HttpEntity entity=null;
try {
resp=hc.execute(get);
entity=resp.getEntity();
String result=EntityUtils.toString(entity);
com.alibaba.fastjson.JSONObject jo=new com.alibaba.fastjson.JSONObject();
return jo.parseObject(result);
}catch (Exception e) {
e.printStackTrace();
}finally {
try { //关闭连接
if(entity!=null)EntityUtils.consume(entity);
if(get!=null)get.abort();
if(hc!=null)hc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
---------------------------------------结束----------------------------------------------
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。