如何在JavaRESTAPI中获取实路径InputStream文件

如何在JavaRESTAPI中获取实路径InputStream文件,java,rest,inputstream,fileinputstream,bufferedinputstream,Java,Rest,Inputstream,Fileinputstream,Bufferedinputstream,我有一个Java Rest API Post方法,它将图像(InputStream)作为参数发送,并且我必须将其保存在Oracle中的blob列中 我需要获取这个输入流的完整路径(真实路径),以便将这个图像保存到数据库中。 我的代码如下 @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file")

我有一个Java Rest API Post方法,它将图像(InputStream)作为参数发送,并且我必须将其保存在Oracle中的blob列中

我需要获取这个输入流的完整路径(真实路径),以便将这个图像保存到数据库中。 我的代码如下

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
                @FormDataParam("file") InputStream uploadedInputStream,
                @FormDataParam("file") FormDataContentDisposition fileDetail) {
String UPLOAD_FOLDER = "c:/uploadedFiles/"; // my rest api does not have this file, how to get at runtime?    
String uploadedFileLocation = UPLOAD_FOLDER + fileDetail.getFileName(); // this line is ok
我想这样做:

String UPLOAD_FOLDER = uploadedInputStream.getRealPathName();


我通过将inputstream转换为字节数组解决了这个问题。我将字节[]转发给数据库持久性方法。我的代码如下:

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();
    ...
}

大多数浏览器不再发送其上载文件的完整原始文件路径(出于安全/隐私原因)。他们只发送没有路径的文件名。为什么需要它?此
InputStream
很可能不是
FileInputStream
,并且很可能与文件没有关联。这篇文章将帮助您理解如何在Oracle中将InputStream写入BLOB列:这篇文章方法将通过android应用程序接收图像。我需要把这个文件写入数据库。文件处理方法(新文件(“path/filename.jpg”)、新文件输入流(“path/filename.jpg”)等)要求提供完整路径
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();
    ...
}