我正在創建一個Web應用程序。用戶上傳ARF文件,我的應用程序將其轉換爲WMV格式。我目前正在對需要轉換的文件名進行硬編碼,爲了避免這種情況,我需要能夠傳入視頻上傳時輸入的文件名,然後只需將擴展名(.arf更改爲.wmv)更改爲轉換。從JSP文件訪問變量到Java Servlets中
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
變量傳遞給此類(並用.wmv替換.arf)。我將如何能夠做到這一點?我想在upload.jsp
文件中公開saveFile
變量,但編譯器不允許我這樣做。
謝謝。只需將其更改爲 Dheeraj