当前位置:Gxlcms > 数据库问题 > java 文件上传数据库

java 文件上传数据库

时间:2021-07-01 10:21:17 帮助过:17人阅读

com.yunztec.eprj.util; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileOperateUtil { private static FileOperateUtil fpu=new FileOperateUtil(); public static FileOperateUtil getInstance(){ return fpu; } public byte[] getBytes(File file){ byte[] buffer = null; try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } public String writeFileToService(byte[] bt, String filePath,String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); System.out.println(dir.exists()); if(!dir.exists()){//判断文件目录是否存在 dir.mkdirs(); } file = new File(filePath+"\\"+fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bt); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return filePath+"\\"+fileName;   } }

 

总结:

1.上传:  file----->byte[]   

2.下载: byte[]------->File---->存到服务器--->提供路径---->下载完成后删除服务器文件。

java 文件上传数据库

标签:sys   mysql   ora   str   pack   style   二进制   filename   out   

人气教程排行