上传视频至本地文件夹(文件也可以上传)java代码

来源:互联网 发布:web前端实战项目源码 编辑:程序博客网 时间:2024/06/14 08:22

视频上传 Controller:

private final static String UPLOADDIR = "/new_resource/video";   //上传文件存放路径    

private List<File> file;  //上传文件集合    

private List<String> fileFileName;   //上传文件名集合   

private List<String> fileContentType;  //上传文件内容类型集合   

 private void uploadFile(int i) throws FileNotFoundException, IOException {   
        try {   
            InputStream in = new FileInputStream(file.get(i));   
            String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);//文件路径
            File fileLocation = new File(dir);  
            //此处也可以在应用根目录手动建立目标上传目录  
            if(!fileLocation.exists()){  
                boolean isCreated  = fileLocation.mkdir();  
                if(!isCreated) {  
                    return;  
                }  
            }  
            String fileName=this.getFileFileName().get(i); 
            File uploadFile = new File(dir, fileName);   
            OutputStream out = new FileOutputStream(uploadFile);  
            byte[] buffer = new byte[1024 * 1024];   
            int length;   
            while ((length = in.read(buffer)) > 0) {   
                out.write(buffer, 0, length);   
            }   
            in.close();   
            out.close();   
        } catch (FileNotFoundException ex) {   
            System.out.println("上传失败!");  
            ex.printStackTrace();   
        } catch (IOException ex) {   
            System.out.println("上传失败!");  
            ex.printStackTrace();   
        }   
    }

//添加视频信息
public String saveVideo() throws Exception {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(0);
Date tjsj = formatter.parse(dateString, pos);
String videoname=fileFileName.get(0);//获取上传文件的名称
TbVideo video = new TbVideo();
video.setVname(vname);
video.setVurl(videoname);
video.setVperson(vperson);
video.setVsoft(vsoft);
video.setPubtime(new Date());
video.setComments(msgEditor);
this.webService.saveVideo(video);

System.out.println(this.getFileFileName());  
        for (int i = 0; i < file.size(); i++) {   
            uploadFile(i);   //循环上传每个文件   
        }   
return SUCCESS;
}


视频上传 jsp

  <tr>
<th width="20%">上传附件:</th>
<td width="42%" >
<label>
<input name="file" id="upFile" type="file"/>
</label><font color="red" style="white-space:nowrap;">*上传视频</font>
</td>
  </tr>