Authored by chaogeng

修改获取html文件流接口

... ... @@ -5,6 +5,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
... ... @@ -16,12 +17,18 @@ import java.io.*;
*/
@RestController
@RequestMapping("/grassHtml")
public class GrassHtmlContentController {
public class HtmlContentController {
static Logger logger = LoggerFactory.getLogger(GrassHtmlContentController.class);
static Logger logger = LoggerFactory.getLogger(HtmlContentController.class);
/**
* 根据路径获取html文件流
* platform 页面回调时使用
* @return byte[]
*/
@RequestMapping("/getHtmlContent")
public byte[] getHtmlContent(@RequestBody String url, HttpServletRequest request){
@ResponseBody
public byte[] getHtmlContent(@RequestBody String url, HttpServletRequest request, HttpServletResponse response){
logger.info("getContentHtml: request url is {}", url);
if(StringUtils.isEmpty(url)){
return null;
... ... @@ -31,15 +38,24 @@ public class GrassHtmlContentController {
}
FileInputStream fis = null;
BufferedOutputStream os = null;
response.setContentType("application/json; charset=UTF-8");
try{
//取context下的html文件转成文件流
String filePath = request.getServletContext().getRealPath(url);
fis = new FileInputStream(filePath);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
logger.info("getContentHtml: request url is {}, result bytes length is {}", url, fis.available());
return buffer;
os = new BufferedOutputStream(response.getOutputStream());
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[2048];
// 将返回的输入流转换成字符串
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
swapStream.write(buffer, 0, len);
}
return swapStream.toByteArray();
}catch (Exception e){
logger.error("getContentHtml: output html bytes failed, url is {}, error is {}", url, e);
}finally {
... ... @@ -50,6 +66,13 @@ public class GrassHtmlContentController {
logger.error("getContentHtml: close inputStream failed, url is {}, error is {}", url, e);
}
}
if(os != null){
try{
os.close();
}catch (Exception e){
logger.error("getContentHtml: close outputStream failed, url is {}, error is {}", url, e);
}
}
}
return null;
}
... ...