转换文件成为二进制数据并保存的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);