1 回答
TA贡献1820条经验 获得超10个赞
我通过将输入流转换为字节数组解决了这个问题。我将 byte[] 转发给数据库持久化方法。我的代码如下:
public byte[] toByteArray(InputStream is) throws IOException{
ByteArrayOutputStream baus = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len= is.read(buffer)) != -1){
baus.write(buffer, 0, len);
}
return baus.toByteArray();
}
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
...
byte[] b = toByteArray(uploadedInputStream);
business.saveUploadedFileInDatabase(b);
...
}
public boolean uploadFile(byte[] b) throws SQLException, IOException{
...
PreparedStatement ps = conn.prepareStatement("INSERT INTO TABLE_IMAGE_TEST (CODE, IMAGE) VALUES (?, ?)");
pstmt.setLong(1, 1L);
pstmt.setBytes(2, b);
pstmt.execute();
...
}
添加回答
举报