Java 如何以字节数组的形式返回jar文件的内容?
我试图以字节数组的形式获取文件的内容Java 如何以字节数组的形式返回jar文件的内容?,java,bytearray,Java,Bytearray,我试图以字节数组的形式获取文件的内容 public static void main(String[] args) { final IServiceClient client = StorageConsumerProvider.getStorageServiceClient("dev"); String name = new File("C:\\Storage\\Model-1.0.0.jar").getName(); StorageObjectIdentifier
public static void main(String[] args) {
final IServiceClient client = StorageConsumerProvider.getStorageServiceClient("dev");
String name = new File("C:\\Storage\\Model-1.0.0.jar").getName();
StorageObjectIdentifier objIdentifier = new StorageObjectIdentifier("Model", "1.0.0", name);
// I need to pass the bytes here
client.createObject(objIdentifier, name.getBytes());
}
界面是这样的-
public void createObject(StorageObjectIdentifier objIdentifier, byte[] obj)
createObject
方法接受两个参数,其中一个参数是-以字节数组形式显示的文件内容
我不确定如何将其作为字节数组传递?有人能帮我吗?在我的例子中,该文件是一个jar文件。您必须使用如下函数手动加载bytearray中的所有文件内容:
public final static byte[] load(FileInputStream fin) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int readCnt = fin.read(readBuf);
while (0 < readCnt) {
baos.write(readBuf, 0, readCnt);
readCnt = fin.read(readBuf);
}
fin.close();
return bout.toByteArray();
}
public最终静态字节[]加载(FileInputStream fin)引发异常
{
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
int readCnt=fin.read(readBuf);
而(0
但如果文件很小,则可以工作。否则,对于大文件,您将运行到NPE。更好的选择是更改传递
InputStream
的接口,而不是byte[]
,并让intf实现者决定如何操作。您必须使用如下函数手动在bytearray中加载所有文件内容:
public final static byte[] load(FileInputStream fin) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int readCnt = fin.read(readBuf);
while (0 < readCnt) {
baos.write(readBuf, 0, readCnt);
readCnt = fin.read(readBuf);
}
fin.close();
return bout.toByteArray();
}
public最终静态字节[]加载(FileInputStream fin)引发异常
{
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
int readCnt=fin.read(readBuf);
而(0
但如果文件很小,则可以工作。否则,对于大文件,您将运行到NPE。更好的选择是更改传递
InputStream
的接口,而不是byte[]
,并让intf实现者决定如何操作。目前,您只传递文件名的字节数组表示形式。您需要打开文件并读取它。您可以使用FileInputStream
等来实现这一点,但如果您愿意使用,则可以轻松地使用:
目前,您只是传递文件名的字节数组表示形式。您需要打开文件并读取它。您可以使用
FileInputStream
等来实现这一点,但如果您愿意使用,则可以轻松地使用:
您正在将包含文件名的字节数组传递给createObject方法。要传递包含文件内容的字节数组,首先必须将文件读入字节数组 使用FileReader或FileInputStream,创建传递文件名或文件对象的实例,
然后使用read()方法之一将字节读入数组 您正在将包含文件名的字节数组传递给createObject方法。要传递包含文件内容的字节数组,首先必须将文件读入字节数组 使用FileReader或FileInputStream,创建传递文件名或文件对象的实例,
然后使用read()方法之一将字节读入数组 您可以使用
BufferedInputStream
将文件作为带缓冲的字节流加载
File iFile = new File("C:\\Storage\\Model-1.0.0.jar");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(iFile));
int read = 0;
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // buffer size
while ((read = bis.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
bis.close();
StorageObjectIdentifier objIdentifier =
new StorageObjectIdentifier("Model", "1.0.0", iFile.getName());
client.createObject(objIdentifier, os.toByteArray());
或者,使用
您可以使用
BufferedInputStream
将文件作为带缓冲的字节流加载
File iFile = new File("C:\\Storage\\Model-1.0.0.jar");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(iFile));
int read = 0;
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // buffer size
while ((read = bis.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
bis.close();
StorageObjectIdentifier objIdentifier =
new StorageObjectIdentifier("Model", "1.0.0", iFile.getName());
client.createObject(objIdentifier, os.toByteArray());
或者,使用
或者使用Java的IO公用程序:或者使用Java的IO公用程序: