【发布时间】:2013-12-30 11:02:57
【问题描述】:
我是 struts2 的新手。
我正在使用 struts2 在系统指令中上传视频文件,但是当我想访问 Html5 文件中上传的内容以播放上传的视频文件时,我无法播放。 我当前在 D:/dk 中上传的目录。但是当我把这条路径放在 html 代码中时它就不起作用了。
动作类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.sql.Connection;
import javax.servlet.ServletContext;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpload1 extends ActionSupport implements ServletContextAware {
private File userImage;
private String userImageContentType;
private String userImageFileName;
private ServletContext context;
public String execute() {
try {
String path = context.getInitParameter("FileUploadPath");
FileInputStream in = null;
FileOutputStream out = null;
in = new FileInputStream(getUserImage());
String destFileName = path;
File destFile = new File(destFileName);
destFile.setReadable(false);
if (!destFile.isDirectory()) {
destFile.mkdir();
}
out = new FileOutputStream(new File(destFile+"\\"+userImageFileName));
byte[] buffer = new byte[1024];
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer,0,c);
}
if (null != in)
in.close();
if (null != out)
out.close();
return "success";
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return "input";
}
}
public File getUserImage() {
return userImage;
}
public void setUserImage(File userImage) {
this.userImage = userImage;
}
public String getUserImageContentType() {
return userImageContentType;
}
public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
}
public String getUserImageFileName() {
return userImageFileName;
}
public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
}
@Override
public void setServletContext(ServletContext arg0) {
context = arg0;
}
}
web.xml
<context-param>
<param-name>FileUploadPath</param-name>
<param-value>D:\Dk</param-value>
</context-param>
<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
index.jsp
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="html5lightbox.js"></script>
</head>
<body>
<a href="C:\Users\asus\Desktop\video1.mp4" class="html5lightbox" title="Altoopa">
<img src="images/Playing-Altoopa.png"/>
</a>
</body>
</html>
我的问题是如何将上传视频的路径放在<a href....> 标签中。我上传的文件位置是D:/Dk/dd.mp4。
【问题讨论】: