TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
File file1 = null
DiskFileUpload diskFileUpload = new DiskFileUpload();
try{
List<FileItem> list = diskFileUpload.parseRequest(request);//将结果集放置在list中
for(FileItem fileItem : list){
//如果是文本域
if(fileItem.isFormField()){
if("FileType".equals(fileItem.getFieldName())){
fileType = new String(fileItem.getString().getBytes(),"UTF-8");
}
if("FileName".equals(fileItem.getFieldName())){
fileName = new String(fileItem.getString().getBytes(),"UTF-8");
}
if("FileDescription".equals(fileItem.getFieldName())){
fileDescription = new String(fileItem.getString().getBytes(),"UTF-8");
}
}//如果是文件域
else{
File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
fileForm = remoteFile.getName().substring(remoteFile.getName().lastIndexOf(".")+1, remoteFile.getName().length());//获取文件的后缀名
file1 = new File(this.getServletContext().getRealPath("uploadedFiles"),new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"."+fileForm);
file1.getParentFile().mkdirs(); //创建文件夹路径
file1.createNewFile(); //创建新文件
fileUrl = "uploadedFiles/"+file1.getName(); //获取上传文件的相对路径
fileSize = remoteFile.length()/1024;
//判断是否是以M为单位,还是以Kb为单位
if(fileSize/1024 > 1){
size = remoteFile.length()/1024/1024+"Mb";
}else{
size = fileSize + "Kb";
}
InputStream ins = fileItem.getInputStream();//FileItem的内容
OutputStream ous = new FileOutputStream(file1); //输出到文件夹中
try{
byte[] buffer = new byte[1024]; //字节缓存
int len = 0; //实际缓存长度
while((len=ins.read(buffer)) > -1){
//循环读入缓存m
ous.write(buffer,0,len);
}
}catch(Exception e){}
finally{
ous.close();
ins.close();
}
}
}
}catch(FileUploadException e){} |
|