分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

文件上传

发布时间:2023-09-06 01:20责任编辑:熊小新关键词:文件上传
UploadFilepackage com.sys.CommonUtil;import java.io.BufferedInputStream;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;import java.io.InputStream;public class UploadFile { ???????/** 上传文件 ????* 写Bytes数据到文件 ?????* @param bytes ?????* @param fileName ?????*/ ?????public static boolean writeBytesToFile(byte bytes[], String filePath, ?????????????String fileName) { ?????????boolean rtn = false; ?????????FileOutputStream fos = null; ?????????java.io.File myFilePath = null; ?????????try { ?????????????myFilePath = new java.io.File(filePath); ?????????????if (!myFilePath.exists()) { ?????????????????myFilePath.mkdir(); ?????????????} ?????????????fos = new FileOutputStream(filePath + fileName); ?????????????fos.write(bytes); ?????????????fos.flush(); ?????????????rtn = true; ?????????} catch (FileNotFoundException e) { ?????????????e.printStackTrace(); ?????????} catch (IOException e) { ?????????????e.printStackTrace(); ?????????} finally { ?????????????myFilePath = null; ?????????????try { ?????????????????fos.close(); ?????????????} catch (IOException iex) { ?????????????????iex.printStackTrace(); ?????????????} ?????????} ?????????return rtn; ?????} ?????/** ?????* 获得指定文件的byte数组 ?????*/ ?????public static byte[] getBytes(String filePath){ ?????????byte[] buffer = null; ?????????try { ?????????????File file = new File(filePath); ?????????????FileInputStream fis = new FileInputStream(file); ?????????????ByteArrayOutputStream bos = new ByteArrayOutputStream(); ?????????????byte[] b = new byte[1024]; ?????????????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; ?????} ????????????/** ????????* ?????????* @param f 上传的文件 ????????* @param outpath 上传路径 ????????* @param outname 文件名称 ????????*/ ???????public static void upload(File f,String outpath,String outname) { ?????????????BufferedInputStream bis = null; ?????????????BufferedOutputStream bos = null; ?????????????try { ?????????????????InputStream datas=new FileInputStream(f); ???????????????//获取客户端传递的InputStream ?????????????????bis = new BufferedInputStream(datas); ?????????????????//创建文件输出流 ?????????????????bos = new BufferedOutputStream(new FileOutputStream(outpath+outname)); ?????????????????byte[] buffer = new byte[8192]; ?????????????????int r = bis.read(buffer, 0, buffer.length); ?????????????????while (r > 0) { ?????????????????????bos.write(buffer, 0, r); ?????????????????????r = bis.read(buffer, 0, buffer.length); ?????????????????} ?????????????????System.out.println("-------文件上传成功!-------------"); ?????????????} catch (IOException e) { ?????????????????throw new RuntimeException(e); ?????????????} finally { ?????????????????if (bos != null) { ?????????????????????try { ?????????????????????????bos.close(); ?????????????????????} catch (IOException e) { ?????????????????????????throw new RuntimeException(e); ?????????????????????} ?????????????????} ?????????????????if (bis != null) { ?????????????????????try { ?????????????????????????bis.close(); ?????????????????????} catch (IOException e) { ?????????????????????????throw new RuntimeException(e); ?????????????????????} ?????????????????} ?????????????} ?????????} ??????/**、 ?????* ?????????* @param path 上传路径 ?????* @param name 上传名称 ?????* @param datas 上传类容 ?????*/ ???public void Upload(String path,String name,String datas){ ???????try { ???????????FileOutputStream fos = new FileOutputStream(path+name); ???????????InputStream is = new FileInputStream(datas); ???????????byte[] buffer = new byte[8192]; ???????????int count = 0; ???????????while ((count = is.read(buffer)) > 0) { ???????????????fos.write(buffer, 0, count); ???????????} ???????????fos.close(); ???????????is.close(); ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????} ???} ???????????public static void main(String[] args) { ???????File f = new File("c:/log/123.txt"); ???????upload(f, "c:/log/", "123.mp4"); ???} ???}

文件上传

原文地址:http://www.cnblogs.com/syscn/p/7742395.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved