j2me 接收图片问题
publicvoidgetpic(Graphicsg)throwsIOException{finalStringURL="http://localhost:8081/tr...
public void getpic(Graphics g) throws IOException
{
final String URL = "http://localhost:8081/tryservlet/Test";
HttpConnection httpConn = null;
InputStream is = null;
try
{
httpConn = (HttpConnection) Connector.open(URL);
httpConn.setRequestMethod(HttpConnection.POST);
if(httpConn.getResponseCode()==HttpConnection.HTTP_OK){
DataInputStream dis=httpConn.openDataInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] imageData;
int len=(int)httpConn.getLength();
if(len!=-1){
imageData=new byte[len];
dis.readFully(imageData);
}
else {
int ch = 0;
while ((ch = dis.read()) != -1)
{
baos.write(ch);
}
imageData = baos.toByteArray();
}
Image image = Image.createImage(imageData, 0, imageData.length);
g.drawImage(image,0,0,Graphics.VCENTER | Graphics.HCENTER);
baos.close();
dis.close();
}
is.close();
httpConn.close();
} catch (IOException e)
{System.out.println(e.toString());
e.printStackTrace();
}
}
这段方法代码哪里有问题呢??使用它的时候老是说Uncaught exception java/lang/IllegalArgumentException: .
烦很久啦郁闷!! 展开
{
final String URL = "http://localhost:8081/tryservlet/Test";
HttpConnection httpConn = null;
InputStream is = null;
try
{
httpConn = (HttpConnection) Connector.open(URL);
httpConn.setRequestMethod(HttpConnection.POST);
if(httpConn.getResponseCode()==HttpConnection.HTTP_OK){
DataInputStream dis=httpConn.openDataInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] imageData;
int len=(int)httpConn.getLength();
if(len!=-1){
imageData=new byte[len];
dis.readFully(imageData);
}
else {
int ch = 0;
while ((ch = dis.read()) != -1)
{
baos.write(ch);
}
imageData = baos.toByteArray();
}
Image image = Image.createImage(imageData, 0, imageData.length);
g.drawImage(image,0,0,Graphics.VCENTER | Graphics.HCENTER);
baos.close();
dis.close();
}
is.close();
httpConn.close();
} catch (IOException e)
{System.out.println(e.toString());
e.printStackTrace();
}
}
这段方法代码哪里有问题呢??使用它的时候老是说Uncaught exception java/lang/IllegalArgumentException: .
烦很久啦郁闷!! 展开
展开全部
有两个地方可能会抛出这个异常:
1。image = Image.createImage(imageData, 0, imageData.length);
IllegalArgumentException - if imageData is incorrectly formatted or otherwise cannot be decoded 图片格式不对
2。
g.drawImage(image,0,0,Graphics.VCENTER | Graphics.HCENTER);
IllegalArgumentException - if anchor is not a legal value 锚点(即最后一个参数)非法 换成 Graphics.LEFT|Graphics.TOP试下
1。image = Image.createImage(imageData, 0, imageData.length);
IllegalArgumentException - if imageData is incorrectly formatted or otherwise cannot be decoded 图片格式不对
2。
g.drawImage(image,0,0,Graphics.VCENTER | Graphics.HCENTER);
IllegalArgumentException - if anchor is not a legal value 锚点(即最后一个参数)非法 换成 Graphics.LEFT|Graphics.TOP试下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询