Java ng src从映像应用程序/jpg中删除请求头响应分隔符

Java ng src从映像应用程序/jpg中删除请求头响应分隔符,java,angularjs,Java,Angularjs,我正在使用长轮询动态加载图像。但返回的结果似乎总是一个json分隔的头 控制台输出: (unable to decode value)" ,"1072":"�","1073":"�","1074":"�","1075":"�","1076":"\u0004","1077":"}","1078":"\u0000","1079":"�","1080":"�","1081":"�","1082":" html: 爪哇: @RequestMapping(value=“/agent/getOctetS

我正在使用长轮询动态加载图像。但返回的结果似乎总是一个json分隔的头

控制台输出:

(unable to decode value)"
,"1072":"�","1073":"�","1074":"�","1075":"�","1076":"\u0004","1077":"}","1078":"\u0000","1079":"�","1080":"�","1081":"�","1082":" 
html:

爪哇:

@RequestMapping(value=“/agent/getOctetStream/{clientId}”,
method=RequestMethod.POST,
产生=MediaType.IMAGE\u JPEG\u值)
public@ResponseBody ResponseEntity getOctetStream(
@PathVariable字符串(clientId)引发IOException{
File File=新文件(“C:\\logos\\Logo\u BW.jpg”);
DataInputStream=新DataInputStream(新文件InputStream(文件));
字节[]缓冲区=新字节[18192];
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
int字节读取;
而((bytesRead=stream.read(buffer))!=-1)
{
写入(缓冲区,0,字节读取);
}
HttpHeaders=新的HttpHeaders();
setContentType(新媒体类型(“应用程序”、“jpg”));
headers.setContentLength(baos.toByteArray().length);
ResponseEntity ResponseEntity=新的ResponseEntity(baos.toByteArray(),headers,HttpStatus.CREATED);
logger.debug(“responseEntity:+responseEntity”);
返回响应性;}

Angular无法解释二进制字符串。它必须以带有分隔符的json格式发送,因此您必须创建自己的分隔符。字符串image9j=Base64.encodeBase64String(字节);ImageMap.put(“图像”,image9j)

爪哇

html


<div ng-controller="TestCtrl"><img ng-src="data:image,{{getBinary}}" /></div>
App.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.common['Accept'] = 'application/jpg, application/json, text/plain, */*';
}]);

var getBinary = $resource(
"http://:host::port/" + context + "/agent/getOctetStream/:clientId",
{host: WEB_SERVER_NAME, port: WEB_SERVER_PORT,clientId: clientId, }
);
$scope.getBinary  =  getBinary.save();
@RequestMapping(value="/agent/getOctetStream/{clientId}",
method = RequestMethod.POST,
produces = MediaType.IMAGE_JPEG_VALUE ) 
public @ResponseBody ResponseEntity<byte []> getOctetStream(
@PathVariable String clientId) throws IOException {

File file = new File("C:\\logos\\Logo_BW.jpg");
DataInputStream stream = new DataInputStream(new FileInputStream(file)); 
byte[] buffer = new byte[18192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1)
{
baos.write(buffer, 0, bytesRead);
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "jpg"));
headers.setContentLength(baos.toByteArray().length);
ResponseEntity responseEntity = new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
logger.debug("responseEntity :" + responseEntity);
return responseEntity;}
@RequestMapping(value="/agent/getImageString/{clientId}",
method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE )
public @ResponseBody String  getImageString(                            @PathVariable String clientId) throws IOException {
File file = new File("C:\\Logo_BW.jpg");
DataInputStream stream = new DataInputStream(new FileInputStream(file)); 
byte[] buffer = new byte[18192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1)
{
    baos.write(buffer, 0, bytesRead);
}
byte[] bytes = baos.toByteArray();
Gson gson = new Gson(); String json;
HashMap<String, Object> ImageMap = new HashMap<String, Object>();
String image9j = Base64.encodeBase64String(bytes); // creates a sting "/9j/4AAQSkZJRgABA..."
ImageMap.put("image", image9j);  // the delimiter json label.
json = gson.toJson(ImageMap);
json = "[" + json + "]"; //Must have this to work!!
return json;}
var getImageJson = $resource(
"http://:host::port/" + context + "/agent/getImageString/:clientId",
{host: WEB_SERVER_NAME, port: WEB_SERVER_PORT,clientId: clientId, }
);
$scope.getImageJson  =  getImageJson.query();
<img ng-repeat="im in getImageJson" ng-src="data:image/jpg;base64,{{im.image}}" />