【问题标题】:Empty file received using HTTP Post method使用 HTTP Post 方法接收到的空文件
【发布时间】:2018-04-07 22:11:20
【问题描述】:

我正在尝试使用 HTTP 下载文件,这是代码。
有了这个,我有一个使用正确名称创建的目录,以及使用正确名称创建的目录中的一个文件,但文件中没有写入任何内容。

PostMethod post = new PostMethod(serverUrl);
post.setRequestEntity(entity);
httpclient.executeMethod(post); 

File contentDirectory = new File(fileFullPath);
if(contentDirectory.exists() == false){
    contentDirectory.mkdir();
}

File localFile = new File(fileFullPath + File.separator + filename);

int readBuf = 0;
byte[] buf = new byte[Utils.getBufferSize()];   (BufferSize Checked)
InputStream is = null;  

is = post.getResponseBodyAsStream();
FileOutputStream fos = new FileOutputStream(localFile);

while((readBuf = is.read(buf))!= -1){
    fos.write(buf, 0, readBuf);
    logger.info("readBuf : "+readBuf);
}

is.close();
fos.close();enter code here

if(localFile.exists()) Transfer_Success = true;

【问题讨论】:

    标签: http inputstream


    【解决方案1】:

    作为一个菜鸟,我一直在将 post 方法发送到错误的 servlet。只有新手才会犯的错误。

    所以我正确传输了字节,但是这次图像文件由于编码类型错误或其他原因无法打开。我正在解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多