热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

文件上传(记得关闭流)

form:<basicnamesmwdlabel导入widgetFileUploaderreferWidgetsparam><valuenam
form:

name="smwd" label="导入" widget="FileUploader" referWidgets="param">
name="destform">fwglAdd
name="action">upload
name="fileTypes">*.doc;*.docx
name="fileAmount">1
action:

try {
Map, File> l = (Map) bus.getValue("smwd");
if (l == null || l.isEmpty())
return;
for (String key : l.keySet()) {

System.out.println(" file name is: " + l.keySet());

File file = (File) l.get(key);
FileInputStream in = new FileInputStream(file);
String guid = java.util.UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
String d = System.getProperty("file.separator");//代表一个"/"
String path1 = String.valueOf(FileUpLoadAction.class.getResource(""));
System.out.println(path1.substring(6, path1.length()));
String os1 = System.getProperty("os.name");
if(os1.toLowerCase().startsWith("win")){//判断是不是windows操作系统
File writeFile = new File(path1.substring(6, path1.length()) + "upload" + d + guid + "_" + key);
FileOutputStream os = new FileOutputStream(writeFile);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) != -1) {
os.write(b, 0, i);
}
os.flush();//很重要
form.updateWidgetValue("path", writeFile);
form.updateWidgetValue("smwdmc", key);
in.close();//很重要
os.close();//很重要
}else{//如果是linux操作系统,路径前边要多加一个 "/"
File writeFile = new File(d+path1.substring(6, path1.length()) + "upload" + d + guid + "_" + key);
FileOutputStream os = new FileOutputStream(writeFile);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) != -1) {
os.write(b, 0, i);
}
os.flush();//很重要
form.updateWidgetValue("path", writeFile);
form.updateWidgetValue("smwdmc", key);
in.close();//很重要
os.close();//很重要
}


}

} catch (Exception e) {

e.printStackTrace();

}

推荐阅读
author-avatar
Sophie-wyq_661
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有