我通过为 Blob 创建一个 Provider 来解决它,从 InputSTream-Provider 采用
@Provider
@Produces("*/*")
public class BlobProvider implements MessageBodyWriter<Blob> {
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Blob.class.isAssignableFrom(type);
}
public long getSize(Blob blob, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
public void writeTo(Blob blob, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
SessionFactory sessionFactory = (SessionFactory) Utils.getBean("sessionFactory");
sessionFactory.getCurrentSession().beginTransaction();
InputStream inputStream = null;
try {
inputStream = blob.getBinaryStream();
int c = inputStream.read();
if (c == -1) {
httpHeaders.putSingle(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(0));
entityStream.write(new byte[0]); // fix RESTEASY-204
return;
} else
entityStream.write(c);
ProviderHelper.writeTo(inputStream, entityStream);
} catch (SQLException e) {
Utils.logError(e);
} finally {
if (inputStream != null)
inputStream.close();
}
sessionFactory.getCurrentSession().getTransaction().commit();
}
}