您的位置:首页 > 编程语言 > Java开发

java IO流实现文件上传的demo

2013-05-09 19:21 363 查看
//文件上传
//FileName 为文件名
//file 为文件
//path 为文件的路径,最好是绝对路径
private static void upLoad(String path,String FileName,File file) throws Exception{
File temp = new File(ServletActionContext.getServletContext().getRealPath(path),FileName);
temp.getParentFile().mkdirs();
InputStream ins = null;
OutputStream ous = null;
ins = new FileInputStream(file);
ous = new FileOutputStream(temp);
byte[] bytes = new byte[1024];
int length = 0;
while ((length = ins.read(bytes)) != -1) {
ous.write(bytes, 0, length);
}
if (ous != null) {
ous.close();
}
if (ins != null) {
ins.close();
}
}

本文出自 “码农今天不上班” 博客,请务必保留此出处http://wangpeng1206.blog.51cto.com/6941287/1196745
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: