我是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
。
浏览器仅访问Web应用程序公开的内容,而不访问服务器硬盘驱动器上的任意位置-想象提供对服务器的全权访问意味着什么?
相反,您需要从操作中流式传输它,例如,使用stream
结果类型,或将服务器配置为允许访问特定资产目录等。
您的锚标记的href需要指向操作,并提供足够的信息来检索文件。您可以通过URL参数提供文件名,使用REST风格的参数等。
如果直接写响应(IMO是个坏主意),则需要设置内容类型以及可能的长度等内容。不过,我认为stream
结果类型会更好。
无关,但是您的动作课做得太多;文件IO工作应在execute
方法之外的其他地方完成,例如在服务或实用程序类中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句