簡體   English   中英

從InputStream創建文件

[英]Creating file from an InputStream

我正在嘗試將客戶端提供的文件復制到臨時文件中。

這個給定的文件由我的REST服務檢索到InputStream中。

這是我的代碼:

@POST
@Path("fileupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("uploadFormElement") InputStream uploadedInputStream,
    @FormDataParam("uploadFormElement") FormDataContentDisposition fileDetail)
    throws IOException {

Response.Status respStatus = Response.Status.OK;

if (fileDetail == null) {
    respStatus = Response.Status.INTERNAL_SERVER_ERROR;
} else {
    if (uploadedInputStream != null) {
    try {
        initPath();
        int size = 0;
        int bytesRead;
        boolean isStreamSizeCorrect = true;
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        while ((bytesRead = uploadedInputStream.read(buffer)) != -1) {
        if (size > OntoWebStudioUtil.getUploadFileLimit()) {
            isStreamSizeCorrect = false;
            baos.close();
            while ((bytesRead = uploadedInputStream.read(buffer)) != -1) {
            size++;
            }
            break;
        } else {
            baos.write(buffer, 0, bytesRead);
        }
        size++;
        }
        if (!isStreamSizeCorrect) {
        respStatus = Response.Status.NOT_ACCEPTABLE;
        return Response
            .status(respStatus)
            .entity("Size of uploaded file exceeds the limit"
                + OWSConstants.UPLOAD_RESPONSE_VALUE_SEPARATOR
                + size).build();
        }
        byte[] outBuf = baos.toByteArray();

        String newFilePath = "C:\\Docs\\my_pic.png";
        FileOutputStream fos = null;
        try {
        fos = new FileOutputStream(newFilePath);
        fos.write(outBuf);
        }
        catch (IOException e) {
            e.printStackTrace(System.err);
        }
         finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
         }
    } catch (Exception e) {
        respStatus = Response.Status.INTERNAL_SERVER_ERROR;
        e.printStackTrace(System.out);
    }
    }
}
return Response
    .status(respStatus)
    .entity(tempFileName
        + OWSConstants.UPLOAD_RESPONSE_VALUE_SEPARATOR
        + currentFileName).build();
}
}

我的問題是在我的臨時目錄中創建的文件為空。 創建文件之前,我必須測試文件的大小。 這就是我閱讀InputStream的原因。 因此,我嘗試在閱讀過程中對其進行復印。 但這是行不通的。 我能做什么 ?

謝謝

如果您使用的是Java SE 7或更高版本,則可以使用Files.copy

該代碼基本上是正確的。 因此,唯一的結論是: is.read(bytes)被調用,但返回-1,因此InputStream is被讀取一次到流末尾。

還有第二種可能性,即e.printStackTrace()將轉到您未看到的位置( System.err )。 如果您確實看到Done! ,請嘗試e.printStackTrace(System.out)

數據中為-1沒問題。

解決方案:我發現了一些似乎可行的方法。 也許不是很好,所以歡迎其他建議! 在那里:我聲明

ByteArrayOutputStream baos = new ByteArrayOutputStream();

我在閱讀InputStream並檢查大小時寫了它

while ((bytesRead = uploadedInputStream.read(buffer)) != -1) {
if (size > OntoWebStudioUtil.getUploadFileLimit()) {
    isStreamSizeCorrect = false;
    baos.close();
    while ((bytesRead = uploadedInputStream.read(buffer)) != -1) {
        size++;
    }
        break;
} else {
    baos.write(buffer, 0, bytesRead);
}
size++;
}

然后,我聲明一個ByteArrayInputStream以將我寫的內容寫入ByteArrayOutputStream中。 最后,我使用Apache commons-io復制方法。

fos = new FileOutputStream(newFilePath);
ByteArrayInputStream newStream = new ByteArrayInputStream(baos.toByteArray());
IOUtils.copy(newStream, fos);

我在調用此方法之前聲明了FileOutputStream以便關閉FileOutputStream。 如果文件未關閉,我們將無法讀取。

希望它能對其他人有所幫助:)謝謝您的寶貴時間!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM