我正在创建一个Web应用程序。用户上传ARF文件,我的应用程序将其转换为WMV格式。我目前正在对需要转换的文件名进行硬编码,为了避免这种情况,我需要能够传入上传视频时输入的文件名,然后只需更改扩展名(.arf更改为.wmv)转换。
upload.jsp
检索上传文件的名称并执行转换:
<%@ page import="java.io.*"%>
<%
String saveFile = "";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
String saveFile2 = "C:/Webex/" +saveFile;
File ff = new File(saveFile2);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
try {
Process p = Runtime.getRuntime().exec("cmd /C C:/Users/dheerajg/Desktop/webex2.vbs");
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
}
}
%>
<table border="2">
<tr>
<td><b>You have successfully converted the file by the name of:</b>
<%
out.println(saveFile);
%>
</td>
</tr>
</table>
<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="download.jsp" METHOD=POST>
<br> <br> <br>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Download the recording"></td>
</tr>
</FORM>
</BODY>
</HTML>
注意:String saveFile
包含上传文件的文件名。转换转换后,将调用download.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Download Servlet Test</title>
</head>
<body>
Click on the link to download: <a href="DownloadServlet">Download Link</a>
</body>
</html>
当用户点击下载链接时,它会执行Servlet DownloadServlet.java
:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private static final int BUFSIZE = 4096;
private String filePath;
public void init() {
filePath = "C:/Webex/DEMO-20131128 2211-1(1).wmv"; // Notice how the name of the converted video has been hardcoded.
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
File file = new File(filePath);
int length = 0;
ServletOutputStream outStream = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(filePath);
// sets response content type
if (mimetype == null) {
mimetype = "application/octet-stream";
}
response.setContentType(mimetype);
response.setContentLength((int)file.length());
String fileName = (new File(filePath)).getName();
// sets HTTP header
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] byteBuffer = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(file));
// reads the file's bytes and writes them to the response stream
while ((in != null) && ((length = in.read(byteBuffer)) != -1))
{
outStream.write(byteBuffer,0,length);
}
in.close();
outStream.close();
}
}
为避免转换后的文件被硬编码,我希望能够将saveFile
变量传递给此类(并将.arf替换为.wmv)。我怎么能这样做?我想在saveFile
文件中公开upload.jsp
变量,但编译器不允许我这样做。
答案 0 :(得分:1)
你应该将输出文件名作为url参数传递
在upload.jsp
中<%
String resultFile = saveFile.replaceAll(".arf", ".wmv");
%>
<html>
<body>
Your file successfully converted<br />
Click <a href="downloadservlet.jsp?f=<%= resultFile %>">here</a> to download<br />
</body>
在downloadServlet doGet()中的
resultFile = request.getParameter("f");
filePath = "C:/Webex/" + resultFile;
根本不需要download.jsp。
答案 1 :(得分:0)
假设您希望将文件下载限制为上载它的用户,您可以使用文件路径字符串值存储会话属性。在下载servlet中,您可以引用相同的会话属性并将其值用作文件系统路径。
upload.jsp:
session.setAttribute("filePath", saveFile);
在DownloadServlet.java中,删除实例变量声明(您希望这是每个用户,不在所有用户之间共享)filePath
,然后在doGet()
内添加:
String filePath = (String) session.getAttribute("filePath");
如果没有填充,请务必检查它是否为空。