android media_type_image,android - Retrofit image upload returns 415 Unsupported Media Type - Stack ...

博主在尝试使用 JBoss 和 RestEasy 创建的 REST 资源进行 Android 项目中的图像上传。他们定义了一个使用 Retrofit 的接口,并通过 ByteArray 输出流压缩图片数据。然而,服务返回了 415 错误,意味着不支持的媒体类型。问题可能在于请求头的设置或内容类型的匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a REST Resource using JBoss & RestEasy as follows

@Path(ServiceURL.MEDIA)

public class ImageUploadService {

@POST

@Consumes({APPLICATION_OCTET_STREAM})

public Response upload(@QueryParam("contenttype") String contentType, InputStream input, @Context HttpServletRequest request) throws IOException {

...

}

}

and a Retrofit interface in an Android project as follows:

public interface ImageUploadService {

@Multipart

@POST(ServiceURL.BASE + ServiceURL.MEDIA)

public Response upload(@Query("contenttype") String contentType, @Part("image") TypedByteArray input);

}

which is invoked via

ByteArrayOutputStream bos = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);

byte[] bitmapData = bos.toByteArray();

RestAdapter ra = new RestAdapter.Builder()

.setEndpoint(hostURL)

.setClient(new MediaClient())

.build();

ImageUploadService imageUploadService = ra.create(ImageUploadService.class);

TypedByteArray typedByteArray = new TypedByteArray(MediaType.APPLICATION_OCTET_STREAM, bitmapData);

imageUploadService.upload("image/png", typedByteArray);

and the MediaClient is

public class MediaClient extends OkClient {

@Override

protected HttpURLConnection openConnection(Request request) throws IOException {

HttpURLConnection connection = super.openConnection(request);

connection.setRequestMethod(request.getMethod());

connection.setRequestProperty("Accept", MediaType.APPLICATION_OCTET_STREAM);

return connection;

}

}

However the service returns a 415 Unsupported Media Type and the method body of the service resource is not invoked, implying RestEasy is rejecting the request.

Any ideas?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值