我解決了這個問題,adopded從InputStream的提供商
@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();
}
}
爲什麼你認爲你不能從另一個讀取BLOB連接? –
,因爲當我嘗試從它讀取時,我得到異常「連接關閉」 – wutzebaer