導航:首頁 > 編程語言 > javafiletobyte

javafiletobyte

發布時間:2023-03-01 02:50:08

java里怎樣把文件轉換成二進制

轉換文件成為二進制數據並保存的Java代碼

取出數據並還原文件到本地的java代碼:

[java]view plain//讀取資料庫二進制文件

publicvoidreaderJpg()throwsSQLException

{

connection=connectionManager.getconn();//自己連接自己的資料庫

StringsqlString="selectimagesfromsave_imagewhereid=4";//從資料庫中讀出要還原文件的二進制碼,這里我讀的是自己的資料庫id為4的文件

Filefile=newFile("E:\1.jpg");//本地生成的文件

if(!file.exists())

{

try{

file.createNewFile();

}catch(Exceptione){

e.printStackTrace();

}

}

try{

byte[]Buffer=newbyte[4096*5];

statement=connection.prepareStatement(sqlString);

resultSet=statement.executeQuery();

if(resultSet.next())

{

FileOutputStreamoutputStream=newFileOutputStream(file);

InputStreamiStream=resultSet.getBinaryStream("images");//去欄位用getBinaryStream()

intsize=0;

while((size=iStream.read(Buffer))!=-1)

{

System.out.println(size);

outputStream.write(Buffer,0,size);

}

}

}catch(Exceptione){

e.printStackTrace();

}

}

② java中文件上傳 new File(文件路徑)問題

通過 」new FileInputStream(文件路徑)「的形式進行上傳即可。舉例:
/**
* 加密文件
*
* @param fileName
* @param date
* @param plainFilePath 明文文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String encodeAESFileUploadByFtp(String plainFilePath, String fileName, String date,String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上傳文件開始");
byte[] bytes = encodeAES(key, bos.toByteArray());
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
Log.info("連接遠程上傳伺服器"+CMBCUtil.CMBCHOSTNAME+":"+2021);
ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
// Log.info("連接遠程上傳伺服器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CMBCHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
ftpClient.makeDirectory(date);
ftpClient.changeWorkingDirectory("/"+filepath+"/" + date);
}
}
Log.info("檢查文件路徑是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, is);
Log.info("加密上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/" + date);
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}

}

③ java中如何把一個文件轉化為byte數組

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

public class Test {

public static void main(String[] args) {

// TODO Auto-generated method stub

try{

getBytesFromFile(new File("C:\\aaa.txt"));

}catch(IOException e){

System.out.println("IOException");

}

}

// 返回一個byte數組

public static byte[] getBytesFromFile(File file) throws IOException {

InputStream is = new FileInputStream(file);

// 獲取文件大小

long length = file.length();

if (length > Integer.MAX_VALUE) {

// 文件太大,無法讀取

throw new IOException("File is to large "+file.getName());

}

// 創建一個數據來保存文件數據

byte[] bytes = new byte[(int)length];

// 讀取數據到byte數組中

int offset = 0;

int numRead = 0;

while (offset < bytes.length

&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {

offset += numRead;

}

// 確保所有數據均被讀取

if (offset < bytes.length) {

throw new IOException("Could not completely read file "+file.getName());

}

// Close the input stream and return bytes

is.close();

return bytes;

}

}

④ Java批量寫文件速度越來越慢

可能是你頻繁創建並寫文件導致磁碟跟不上了吧..,你在for循環裡面加個Thread.sleep(1000),每版個文權件的復制時間應該不會出超過1秒的了.另外我用固態硬碟試了試,後面幾個也會變慢,不過好一點,300多ms.

⑤ 有關java byte類型的二維數組問題

首先:無
其次:
guave
Files.toByteArray(File)
ByteStreams.toByteArray(InputStream)

apache commons
IOUtils.toByteArray(InputStream input)

⑥ Java中圖片轉換成位元組流用哪些個類

publicbyte[] SetImageToByteArray(string fileName)

{
FileStream fs =newFileStream(fileName, FileMode.Open);

intstreamLength = (int)fs.Length;byte[] image =newbyte[streamLength];

fs.Read(image,0, streamLength);

fs.Close();

returnimage;
}
這個能行

⑦ java File 類型 轉換成 MultipartFile

File file = new File("src/test/resources/input.txt");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file",
file.getName(), "text/plain", IOUtils.toByteArray(input));

從國外網站上搜到的,IOUtils.toByteArray(input)不識別時,可直接使用上面 FileInputStream 類型的input做第四個參數也是可以的。
親測有效!

⑧ java中如何將一個zip文件轉為byte數組

input1.read(data1);
這一句執行好data1 就是文件的byte數組了!!!
String str = new String(data1);//轉換成專string
下面不要了屬
for (int length1 = 0; (length1 = input1.read(data1)) > 0;) { output.write(data1, 0, length1); }

⑨ java網路通信如何使用位元組類傳送位元組數據

伺服器端
DataInputStream inData;
socket = serverSocket.accept();
inData = new DataInputStream(socket.getInputStream());
outData = new DataOutputStream(socket.getOutputStream());
byte[] b = ("hello world").getBytes();
outData.write(b,0,b.lenth);
客戶端是
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
byte[] b ;
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
while (in.available() != 0) {
out1.write(in.read());
}
b = out1.toByteArray();
return new String(b);

轉換的時候有問題
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
byte[] b = new byte[in.available()];
for(int i = 0;i < b.length;i++){
b[i] = (byte)in.read();
}

String s = new String(b);

閱讀全文

與javafiletobyte相關的資料

熱點內容
ps3文件分割視頻 瀏覽:280
微信圖片一鍵轉發軟體 瀏覽:331
如何判斷s200plc編程電纜 瀏覽:691
太原編程培訓班哪個好 瀏覽:171
樹葉吹奏教程 瀏覽:6
社交app帶來了哪些社會問題 瀏覽:394
如何安裝愛寶8800數據採集器 瀏覽:712
文件保存了怎麼找不到了 瀏覽:476
彩票網站怎麼辨真假 瀏覽:840
pr找不到該文件 瀏覽:963
java移除panel 瀏覽:354
jsp填充jsp 瀏覽:166
海關外貿大數據在哪裡查 瀏覽:381
思特奇java筆試題 瀏覽:121
葫蘆俠在手機中的文件名 瀏覽:813
plc編程應該怎麼收錢 瀏覽:584
c語言中源文件由什麼組成 瀏覽:890
linuxhttpdphp配置文件 瀏覽:607
拆單數據要怎麼保存 瀏覽:17
mac電腦怎樣壓縮文件到100m 瀏覽:645

友情鏈接