如何使用 POST 或 PUT 在 http 服务器上上传文件的一部分?我可以使用描述的解决方案吗?
我有一个任务。在 http 服务器上上传文件的一部分(我必须跳过文件的开头)。
我该怎么办呢。你知道好的解决方案吗?
我可以使用自己的解决方案吗?安全吗?
我的解决方案。我创建了 FileEntity 的子类,它提供文件流并跳过文件的开头。
我提出了我的实体请求。
HttpPut 请求 = new HttpPut(this.uri);
request.setEntity(new FileOfsetEntity(new File(getDestination()), "binary/octet-stream", offset));
我的 FileOfsetEntity 跳过文件的开头。
class FileOfsetEntity 扩展 FileEntity { 长偏移=0; 公共 FileOfsetEntity(文件文件,字符串内容类型,长偏移量){ 超级(文件,内容类型); this.ofset = 偏移量; } @覆盖 公共长 getContentLength() { 返回 this.file.length() - 偏移量; } @覆盖 公共 InputStream getContent() 抛出 IOException { FileInputStream in = new FileInputStream(this.file); 长跳过 = in.skip(offset); Log.w("FileOfsetEntity.getContent","已跳过 = " + 已跳过); 返回; } @覆盖 public void writeTo(final OutputStream outstream) 抛出 IOException { 如果(输出==空){ throw new IllegalArgumentException("输出流不能为空"); } InputStream instream = new FileInputStream(this.file); 长跳过 = instream.skip(offset); Log.w("FileOfsetEntity.writeTo","已跳过 = " + 已跳过); 尝试 { 字节[] tmp = 新字节[4096]; 整数l; 长读=跳过; while ((l = instream.read(tmp)) != -1) { 读+=l; outstream.write(tmp, 0, l); Log.v("FileOfsetEntity.writeTo",file.getAbsolutePath() + " 已读 = " + 已读 + " 已跳过 = " + 已跳过); } outstream.flush(); } 最后 { instream.close(); } }}
I have a task. Upload on http server part of a file (I must skip beginning of the file).
How can I do it. Do you know good solution?
Can I use my own solution? Is it safe?
My solution. I made subclass of FileEntity that gives file stream and skips beginning of the file.
I put my entity to request.
HttpPut request = new HttpPut(this.uri);
request.setEntity(new FileOfsetEntity(new File(getDestination()), "binary/octet-stream", ofset));
My FileOfsetEntity skips begining of the file.
class FileOfsetEntity extends FileEntity { long ofset = 0; public FileOfsetEntity(File file, String contentType, long ofset) { super(file, contentType); this.ofset = ofset; } @Override public long getContentLength() { return this.file.length() - ofset; } @Override public InputStream getContent() throws IOException { FileInputStream in = new FileInputStream(this.file); long skiped = in.skip(ofset); Log.w("FileOfsetEntity.getContent","skiped = " + skiped); return in; } @Override public void writeTo(final OutputStream outstream) throws IOException { if (outstream == null) { throw new IllegalArgumentException("Output stream may not be null"); } InputStream instream = new FileInputStream(this.file); long skiped = instream.skip(ofset); Log.w("FileOfsetEntity.writeTo","skiped = " + skiped); try { byte[] tmp = new byte[4096]; int l; long readed = skiped; while ((l = instream.read(tmp)) != -1) { readed += l; outstream.write(tmp, 0, l); Log.v("FileOfsetEntity.writeTo",file.getAbsolutePath() + " readed = " + readed + " skiped = " + skiped); } outstream.flush(); } finally { instream.close(); } }}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否会失败或成功。
我建议采用不同的方式来做到这一点。我想您在上传之前已经了解该办公室了。
这是 上传文件的链接到服务器。
在此代码中,移动偏移量,然后开始将其写入流中。
I am not sure this may fail or go through.
I would suggest, a different way of doing this. I presume that you know the office before uploading.
Here is a link to upload file to server.
In this code, move your offset and then start writing it into the stream.