该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//我项目中文件上传的代码
if (file != null) {
String newFileName = null;
long now = new Date().getTime();
String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
File dir = new File(path);
if (!dir.exists())
dir.mkdir();
int index = fileFileName.lastIndexOf('.');
if (index != -1)
newFileName = now + fileFileName.substring(index);
else
newFileName = Long.toString(now);
//System.out.println(newFileName);
reply.setRealfile(fileFileName);//本地系统中的文件名
reply.setMakefile(newFileName); //指定新的名称,根据上传时间
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
FileInputStream fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(new File(dir,
newFileName));
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[(int)file.length()];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
}
catch(IOException e){
e.printStackTrace();
}
finally {
try {
if (null != bis)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (null != bos)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}