上传视频至本地文件夹(文件也可以上传)java代码
2017-10-24 09:25
627 查看
视频上传 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>
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>
相关文章推荐
- 使用java代码在本地创建文件夹(多层目录),在本地磁盘创建文件目录
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)
- java 上传文件代码,支持中文文件名和中文文件内容,可以同时提交多个参数
- Hadoop入门(10)_通过java代码实现从本地的文件上传到Hadoop的文件系统
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)
- hadoop入门之通过java代码实现将本地文件上传到hadoop的文件系统
- Hadoop入门(10)_通过java代码实现从本地的文件上传到Hadoop的文件系统
- java实现sftp客户端上传文件以及文件夹的功能代码
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)
- <实战> 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器 推荐
- 用TreeView控件实现资源管理器(显示本地硬盘下所有文件夹和文件,并可以浏览图片)(示例代码下载)
- 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器
- 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器
- 从本地和远程复制文件java 代码
- XP系统下Java修改文件或文件夹属性的代码。
- 从URL获取文件保存到本地的JAVA代码
- 写入本地路径时,文件是只读的,在前面加上一段代码可以解决这个问题
- 文件上传时生成“日期+随机数”式文件名前缀的Java代码
- java 上传文件到本地
- java读取本地excel文件代码