我有一个文件,可以是任何东西,如ZIP,RAR,TXT,CSV,doc等.我想从它创建一个ByteArrayInputStream.
我正在使用它通过FTPClient从Apache Commons Net 上传文件到FTP .
有谁知道怎么做?
例如:
String data = "hdfhdfhdfhd";
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
Run Code Online (Sandbox Code Playgroud)
我的代码:
public static ByteArrayInputStream retrieveByteArrayInputStream(File file) {
ByteArrayInputStream in;
return in;
}
Run Code Online (Sandbox Code Playgroud) 我有一个ByteArrayInputStream形式的图像.我想把它拿出来,然后把它保存到我文件系统中的某个位置.
我一直在四处走动,你能不能帮助我.
我如何正确地将字节压缩到a ByteArrayOutputStream然后使用ByteArrayInputStream?读取?我有以下方法:
private byte[] getZippedBytes(final String fileName, final byte[] input) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry(fileName);
entry.setSize(input.length);
zipOut.putNextEntry(entry);
zipOut.write(input, 0, input.length);
zipOut.closeEntry();
zipOut.close();
//Turn right around and unzip what we just zipped
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(bos.toByteArray()));
while((entry = zipIn.getNextEntry()) != null) {
assert entry.getSize() >= 0;
}
return bos.toByteArray();
}
Run Code Online (Sandbox Code Playgroud)
当我执行此代码时,底部的断言失败,因为entry.size是-1.我不明白为什么提取的实体与压缩的实体不匹配.
java zip bytearrayoutputstream bytearrayinputstream zipoutputstream
这是代码,但有错误:
bin = new ByteArrayInputStream(socket.getInputStream());
Run Code Online (Sandbox Code Playgroud)
是否可以从套接字接收byte[]使用ByteArrayInputStream?
以下代码摘自核心Java卷1的java Web start章节.
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream printOut = new PrintStream(out);
printOut.print(panel.getText());
//panel.getText() return a String
InputStream data = new ByteArrayInputStream(out.toByteArray());
FileSaveService service = (FileSaveService) ServiceManager
.lookup("javax.jnlp.FileSaveService");
service.saveFileDialog(".", new String[] { "txt" }, data, "calc.txt");
Run Code Online (Sandbox Code Playgroud)
创建了四个对象,流重定向三次.有没有其他方法可以使用jnlp api将数据写入文件?InputStream和ByteArrayInputStream之间有什么区别?
据我所知ByteArrayInputStream,用于读取byte[]数据.我为什么要使用它而不是简单byte[](例如从DB读取).
他们之间有什么不同?
1.i将android摄像头数据转换为bitmao
if (mBitmap == null) //create Bitmap image first time
{
Camera.Parameters params = camera.getParameters();
width_ima = params.getPreviewSize().width;
height_ima = params.getPreviewSize().height;
mBitmap = Bitmap.createBitmap(width_ima, height_ima, Bitmap.Config.RGB_565);
mRGBData = new int[width_ima * height_ima];
}
decodeYUV420SP(mRGBData, data, width_ima, height_ima);
mBitmap.setPixels(mRGBData, 0, width_ima, 0, 0, width_ima, height_ima);
Run Code Online (Sandbox Code Playgroud)
2.用udp发送数据
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteStream); // !!!!!!! change compression rate to change packets size
byte data[] = byteStream.toByteArray();
Log.e(TAG, "SIZE: " + data.length);
int nb_packets = (int) Math.ceil(data.length / (float)DATAGRAM_MAX_SIZE);
int size …Run Code Online (Sandbox Code Playgroud) 我刚决定将Slick2D更改为LibGDX.但是,为了能够将我的游戏移植到LibGDX,我需要帮助了解如何从我的游戏数据文件中创建LibGDX中的纹理.
我的游戏数据文件是加密的,图像是用base64编码的,所以我可以将它们全部保存在一个txt文件中.如何从ByteArrayInputStream创建LibGDX纹理.
在Slick2D中,我将base64字符串转换为bufferedimage.但我不想为LibGDX这样做,因为我可能希望很快就能在Android上获得它.
编辑:我只是想通了,有点儿.我是这样做的:
Texture bucket;
String base64 = "base64 string too long to paste"
byte[] decodedBytes = Base64Coder.decode(base64);
bucket = new Texture(new Pixmap(decodedBytes, 0, decodedBytes.length));
Run Code Online (Sandbox Code Playgroud)
但是,现在图像垂直翻转......我错过了什么吗?
我有一个简单的服务器端代码,它接收一个表示JPEG格式图像的字节数组,并返回图像的尺寸.
public String processImage(byte[] data) {
long startTime = System.currentTimeMillis();
ByteArrayInputStream stream = new ByteArrayInputStream(data);
BufferedImage bufferedImage;
bufferedImage = ImageIO.read(stream);
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
long endTime = System.currentTimeMillis();
return "height="+height+" | width="+width+" | elapsed="+(endTime-startTime);
}
Run Code Online (Sandbox Code Playgroud)
它有效,但问题是它的速度慢得令人无法接受.对于100KB的图像,它需要6s.对于900KB的图像,它需要30秒.这是预期的吗?有没有办法让字节数组更快地进行bufferedImage转换?
仅供参考,抓住高度/宽度并不是我打算做的唯一事情.我最终想要处理bufferedImage.所以获得高度/宽度只是一个示例代码.