码迷,mamicode.com
首页 > Web开发 > 详细

文件上传(记得关闭流)

时间:2017-08-18 14:27:59      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:art   class   substr   doc   cat   output   loader   efi   tput   

form:

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

try {
Map<String, 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();

}

文件上传(记得关闭流)

标签:art   class   substr   doc   cat   output   loader   efi   tput   

原文地址:http://www.cnblogs.com/Allanniloprince/p/7389155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!