我正在尝试从来自移动应用程序的请求属性中获取图像字节。
向上游发送的图像格式为 BitMap。以 Base64 格式编码。
如何获取 Web 应用程序中的字节?我在我的 Web 应用程序中使用以下代码片段,我用它来从移动应用程序请求中获取数据。我对代码感到震惊。我不确定我是对还是错。
byte[] line = null;
int noOfBytesRead;
StringBuffer content = new StringBuffer();
HashMap<String, Object> lResultMap = null ;
try
{
log.info("request getInputStream: " + request.getInputStream());
ServletInputStream sis = request.getInputStream();
line = new byte[128];
noOfBytesRead = sis.readLine(line, 0, 128);
log.info("noOfBytesRead :1 " + noOfBytesRead);
if (noOfBytesRead < 3)
return null;
noOfBytesRead = sis.readLine(line, 0, 128); // Reads the content disposition and form name and filename
log.info("line :"+line+" adn the noOfBytesRead : 2 : " + noOfBytesRead);
int numBytesToRead = noOfBytesRead;
int availableBytesToRead;
while ((availableBytesToRead = sis.available()) > 0)
{
byte[] bufferBytesRead;
bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead] : new byte[availableBytesToRead];
sis.read(bufferBytesRead);
content.append(new String(bufferBytesRead, Charset.forName("iso-8859-1")));
}
sis.close();
log.info("The bytes is : " +content.toString());
byte[] image = null;
image = content.toString().getBytes(Charset.forName("iso-8859-1")) ;
log.info("image : " +image);
Base64Decoder decoder = new Base64Decoder();
byte[] imgBytes = decoder.decode(content.toString());
log.info("imgBytes : decoded byted" + imgBytes);
lResultMap = new HashMap<String, Object>();
lResultMap.put("content" , imgBytes );
log.info("content string buffer : " +content);
log.info("lResultMap : " +lResultMap);
}
catch (Exception e)
{
e.printStackTrace();
log.log(Level.SEVERE, e.getMessage(), e);
}