Java上传方法及相关代码
来源:互联网 发布:java需要学网络编程 编辑:程序博客网 时间:2024/05/16 01:38
//上传
public boolean SaveFile(byte[] binData, String fileName) {
boolean success = false;
File file = new File(fileName);
file.getParentFile().mkdirs();
try {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(file));
bufferedOutputStream.write(binData);
bufferedOutputStream.close();
success = true;
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
//测试方法
public static void main(String[] args) {
FileUpload fu = new FileUpload();
File fileS = new File("f://nod32激活码.txt");
if (!fileS.exists()) {
System.out.println("找不到指定文件");
return;
}
FileInputStream fileIS = null;
byte[] binData = null;
try {
fileIS = new FileInputStream(fileS);
binData = new byte[(int) fileS.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(fileS));
int r = bufferedInputStream.read(binData);
if (r != fileS.length())
throw new IOException("读取文件不正确");
bufferedInputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fileIS != null) {
try {
fileIS.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
boolean f = fu.SaveFile(binData, "c://11.txt");
}
public boolean SaveFile(byte[] binData, String fileName) {
boolean success = false;
File file = new File(fileName);
file.getParentFile().mkdirs();
try {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(file));
bufferedOutputStream.write(binData);
bufferedOutputStream.close();
success = true;
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
//测试方法
public static void main(String[] args) {
FileUpload fu = new FileUpload();
File fileS = new File("f://nod32激活码.txt");
if (!fileS.exists()) {
System.out.println("找不到指定文件");
return;
}
FileInputStream fileIS = null;
byte[] binData = null;
try {
fileIS = new FileInputStream(fileS);
binData = new byte[(int) fileS.length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(
new FileInputStream(fileS));
int r = bufferedInputStream.read(binData);
if (r != fileS.length())
throw new IOException("读取文件不正确");
bufferedInputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fileIS != null) {
try {
fileIS.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
boolean f = fu.SaveFile(binData, "c://11.txt");
}
- Java上传方法及相关代码
- 文件上传相关代码及解释 java web
- Java多文件上传相关代码
- 上传文件相关代码
- android在java代码中动态添加组件及相关布局方法(LayoutParams)
- Java 上传文件过程及清空相关缓存目录
- java中 locale类 及相关方法
- EBS相关代码上传方式
- 软件加密及相关代码
- 软件加密及相关代码
- struts1保存上传图片及文件--Java类代码
- git 上传代码方法
- Java 连接sql 数据库方法及代码
- 文件上传的相关方法
- java版json解释器及相关代码生成
- java-spring文件配置sessionFactory及相关数据源示例代码
- Java 连接到 MySQL 数据库的代码及相关操作
- Java连接到MySQL数据库的代码及相关操作
- css中!important的作用
- Sed修改文件中的某一行
- 记录Hibernate 增删改 执行的SQL语句
- 为cocos2d游戏场景转换添加小菊花
- HDU 3746 Cyclic Nacklace
- Java上传方法及相关代码
- Domino 7.0.2带的JDK版本过低,才1.4.2
- ASP.NET获取客户端、服务器端基础信息集合
- JDK源码分析Java.lang.Boolean的浅析——单例模式的应用(使我们更省内存)
- IT服务管理流程控制的绩效指标 KPI
- Java调用SQL Server的存储过程详解
- Struts中JSP到Action过程中数组的投射
- 情人节京城10大约会点
- poj1286