我有3台机器:
- 文件所在的服务器
- 正在运行REST服务的服务器(Jersey)
- 客户端(浏览器)可访问第二台服务器但无法访问第一台服务器
如何直接(不保存第二台服务器上的文件)将文件从第一台服务器下载到客户机器?
从第二台服务器我可以得到一个ByteArrayOutputStream来从第一台服务器获取文件,我可以使用REST服务将此流进一步传递给客户端吗?
它会这样工作吗?
基本上我想要实现的是允许客户端使用第二台服务器上的REST服务从第一台服务器下载文件(因为没有从客户端到第一台服务器的直接访问)只使用数据流(所以没有数据接触文件第二服务器系统).
我现在尝试使用EasyStream库:
final FTDClient client = FTDClient.getInstance();
try {
final InputStreamFromOutputStream<String> isOs = new InputStreamFromOutputStream<String>() {
@Override
public String produce(final OutputStream dataSink) throws Exception {
return client.downloadFile2(location, Integer.valueOf(spaceId), URLDecoder.decode(filePath, "UTF-8"), dataSink);
}
};
try {
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
StreamingOutput output = new StreamingOutput() {
@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
int length;
byte[] buffer = new byte[1024];
while ((length = isOs.read(buffer)) != -1){
outputStream.write(buffer, 0, …
Run Code Online (Sandbox Code Playgroud) 我的Scala代码从字节流接收二进制文件,它看起来像[61 62 63 64].内容是"abcd".我使用toString将其转换为p,但失败了.如何将其打印为字符串?
到目前为止,我认为它们与由字节组成的字节相同,并且双方需要知道另一侧的字节大小和端点并相应地转换流.然而,维基百科说byte stream
!= bit stream
(https://en.wikipedia.org/wiki/Byte_stream)并且bit streams
专门用于视频编码(https://en.wikipedia.org/wiki/Bitstream_format).在这个RFC https://tools.ietf.org/html/rfc107中,他们讨论了这两件事并进行了描述Two separate kinds of inefficiency arose from bit streams.
.我的问题是:
我在android中使用okhttp库下载文件.我下载成功了.但是当我暂停并恢复下载时出现问题.
Response request = new Request.Builder().url(url).build();
ResponseBody responseBody = response.body();
File file = new File(filePath);
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;
if (isResume) {
output = new FileOutputStream(file, true);
input.skip(downloadedSize);
} else {
output = new FileOutputStream(file, false);
}
long totalByteSize = responseBody.contentLength();
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
downloadedSize += count;
output.write(data, 0, count);
}
Run Code Online (Sandbox Code Playgroud)
问题是例如文件大小是10MB.下载3MB然后恢复下载时暂停,下载完成后文件大小变成13MB.它不是从恢复时的下载大小开始,它从字节流开始下载.所以文件变成13MB.代码有什么问题?
我想在 python 3.x 中将字节类型的对象转换为二进制表示。
例如,我想将字节对象转换为二进制(或十进制 17)b'\x11'
的二进制表示00010001
。
我试过这个:
print(struct.unpack("h","\x11"))
Run Code Online (Sandbox Code Playgroud)
但我得到:
error struct.error: unpack requires a bytes object of length 2
Run Code Online (Sandbox Code Playgroud) 我正在使用 just_audio 插件,它有一个功能描述:从字节流读取。
基本上,当我放置一个文件(来自 url)来播放时,我会保存文件中的字节,因此在这一步之后我想在本地播放它。
我有一个关于如何从字节流播放的问题。谁能提供一个例子如何做到这一点?我需要将其放在我的播放列表中,因此它必须是 ConcatanatingAudioSource 的子项。
我发现的唯一音频源是使用来自 Uri 的音频源。
final _playlist = ConcatenatingAudioSource(
children: [
AudioSource.uri(
Uri.parse(
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
tag: AudioMetadata(
album: "Science Friday",
title: "ddddd",
artwork:
"https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg",
),
)
]
)
Run Code Online (Sandbox Code Playgroud)
这就是我保存字节的方式:
void getBytes() async {
Uri uri = Uri.parse(url);
var rng = new Random();
// get temporary directory of device.
Directory tempDir = await getTemporaryDirectory();
// get temporary path from temporary directory.
String tempPath = tempDir.path;
// create a new file in temporary path with random file …
Run Code Online (Sandbox Code Playgroud) 在为大型(<bloblength><blob>)*
编码二进制文件编写反序列化器时,我陷入了各种 Haskell 生产-转换-消费库的困境。到目前为止,我知道四个流媒体库:
conduit
(Haskell Cast #6conduit
很好地揭示了和之间的差异pipes
)这是一个简单的示例,说明当我尝试使用 进行Word32
流式传输时,会出现问题conduit
。一个稍微更实际的示例将首先读取Word32
确定 blob 长度的 a,然后生成ByteString
该长度的惰性(然后进一步反序列化)。但在这里我只是尝试以流方式从二进制文件中提取 Word32:
module Main where
-- build-depends: bytestring, conduit, conduit-extra, resourcet, binary
import Control.Monad.Trans.Resource (MonadResource, runResourceT)
import qualified Data.Binary.Get as G
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.Lazy as BL
import Data.Conduit
import qualified Data.Conduit.Binary as CB
import qualified …
Run Code Online (Sandbox Code Playgroud) 我目前有一个使用 python-docx 库生成 .docx 文档的程序。
完成 .docx 文件的构建后,我将其保存到字节流中,如下所示
file_stream = io.BytesIO()
document.save(file_stream)
file_stream.seek(0)
Run Code Online (Sandbox Code Playgroud)
现在,我需要将这个word文档转换成PDF。我查看了一些不同的转换库,例如 docx2pdf,甚至使用 comtypes 手动进行转换
import sys
import os
import comtypes.client
wdFormatPDF = 17
in_file = "Input_file_path.docx"
out_file = "output_file_path.pdf"
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
Run Code Online (Sandbox Code Playgroud)
问题是,我需要在内存中进行此转换,并且无法将 DOCX 或 PDF 物理保存到机器上。我见过的每个转换器都需要机器上物理文档的文件路径,但我没有。
有没有办法可以将 DOCX 文件流转换为仅在内存中的 PDF 流?
谢谢
我有一个小应用程序,它使用以下方式读取本地文件:
open(diefile_path, 'r') as csv_file
open(diefile_path, 'r') as file
and also uses linecache module
我需要将用途扩展到从远程服务器发送的文件。
服务器接收到的内容类型是字节。
我找不到很多有关处理 IOBytes 类型的信息,我想知道是否有一种方法可以将字节块转换为类似文件的对象。
我的目标是使用上面指定的 API ( open
, linecache
)
我能够使用 将字节转换为字符串data.decode("utf-8")
,
但我不能使用上面的方法 (open
和linecache
)
一个小例子来说明
data = 'b'First line\nSecond line\nThird line\n'
with open(data) as file:
line = file.readline()
print(line)
Run Code Online (Sandbox Code Playgroud)
输出:
First line
Second line
Third line
Run Code Online (Sandbox Code Playgroud)
可以吗?
我在使用 ServiceCredentials.fromStream() 方法时收到此“java.lang.NoSuchMethodError: com.google.common.io.ByteStreams.exhaust(Ljava/io/InputStream;)J”错误。这里有人遇到过这个问题并且知道解决方法吗?TIA
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>[title]</groupId>
<artifactId>calendar-event-consumer</artifactId>
<version>0.1</version>
<name>${project.artifactId}</name>
<description>Service that listens for events in a queue to push them
to an external API</description>
<inceptionYear>2017</inceptionYear>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.5</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version> …
Run Code Online (Sandbox Code Playgroud) 我有一个几百字节的字符串和一些 Int32 值。我想将这些逐字写入文件。
我尝试了许多建议的解决方案,但没有一个对我有用。我在文件中得到了多余的括号、空格或逗号。
谁能提出一个简单可靠的解决方案?
我以为我的问题很清楚,但在阅读评论后,我简化了它。
如何将“12345”写入和/或附加到文件,以便文件包含以下十六进制值: 3132333435 ?
通过编写 NSData 字符串,我可以获得的最佳结果是 <3132333435>。
我可以使用此链接获得所需的结果:是否 swift 有用于写入字节流的协议?,但我无法将数据附加到我创建的文件中。
我试图在将图像保存到数据库和 S3 之前检测width
它height
。该图像位于bytes
.
这是保存到之前的图像示例Django
ImageField
:
注意:我不想使用ImageField
s height_field
,width_field
因为它由于某种原因极大地减慢了服务器速度,所以我想手动执行。
使用请求下载图像:
def download_image(url):
r = requests.get(url, stream=True)
r.raw.decode_content = True
return r.content
Run Code Online (Sandbox Code Playgroud) 这两个行会我写字母A的file
。有人能告诉我他们在内部工作上有什么不同吗?
FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
fileOutputStream.write(65);
fileOutputStream.write('A');
Run Code Online (Sandbox Code Playgroud)
[编辑]:我对转换在这两种情况下的工作方式更感兴趣。据我所知,ASCII 和 UNICODE 表是什么。
bytestream ×13
python ×4
byte ×3
download ×2
file ×2
java ×2
streaming ×2
string ×2
android ×1
arrays ×1
audio-source ×1
bits ×1
bytestring ×1
django ×1
docx ×1
encoding ×1
file-writing ×1
flutter ×1
google-api ×1
guava ×1
haskell ×1
io ×1
java-io ×1
jersey ×1
just-audio ×1
maven ×1
okhttp ×1
pdf ×1
python-3.x ×1
rest ×1
scala ×1
swift ×1