java 文件上传 与 把文件解析成一个字节数组简单的后台示例
2016-06-08 16:32
846 查看
工具类:
public static void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException {
FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
public static byte[] readFile(File f_file) throws Exception{
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = new FileInputStream(f_file);// pathStr 文件路径
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) {
out.write(b, 0, n);
}// end while
} catch (Exception e) {
throw new Exception("System error,SendTimingMms.getBytesFromFile", e);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}// end try
}// end if
}// end try
byte[] temp = out.toByteArray();
return temp;
}
调用测试:
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream(new File("D:/asd2019-09-09.xls"));
SaveFileFromInputStream(fileInputStream, "E:/", "asd.txt");
byte[] str = readFile(new File("D:/asd2019-09-09.xls"));
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException {
FileOutputStream fs=new FileOutputStream( path + "/"+ filename);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
public static byte[] readFile(File f_file) throws Exception{
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = new FileInputStream(f_file);// pathStr 文件路径
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) {
out.write(b, 0, n);
}// end while
} catch (Exception e) {
throw new Exception("System error,SendTimingMms.getBytesFromFile", e);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}// end try
}// end if
}// end try
byte[] temp = out.toByteArray();
return temp;
}
调用测试:
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream(new File("D:/asd2019-09-09.xls"));
SaveFileFromInputStream(fileInputStream, "E:/", "asd.txt");
byte[] str = readFile(new File("D:/asd2019-09-09.xls"));
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- eclipse无法启动的各种解决方法
- java语言的优缺点
- 基于Java的简易表达式解析工具(一)
- java基础教程--线程
- thrift java eclipse
- RxJava--RxBinding
- MyBatis Spring整合配置映射接口类与映射xml文件
- java基础教程--集合
- 如何优化JAVA中的代码
- Java并发编程-Executor框架之ScheduledThreadPoolExecutor
- Spring3 MVC请求参数获取的几种方法
- Spring3系列11-Spring AOP——自动创建Proxy
- Spring3系列10-Spring AOP——Pointcut,Advisor拦截指定方法
- LeetCode:SubSets
- Java model 对象处理
- Spring3系列9-Spring AOP——Advice
- Spring3系列8-Spring 自动装配 Bean
- Spring3系列7-自动扫描组件或Bean
- Spring3系列6-Spring 表达式语言(Spring EL)
- java基础教程-对象