2014-01-10 70 views
0

我正在創建一個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變量,但編譯器不允許我這樣做。

回答

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

所有。

+0

謝謝。只需將其更改爲 Dheeraj

0

假設您想限制向上傳文件的用戶下載文件,可以使用文件路徑字符串值存儲會話屬性。在您的下載servlet中,您可以引用相同的會話屬性並將其值用作文件系統路徑。

upload.jsp:

session.setAttribute("filePath", saveFile); 

在DownloadServlet.java,刪除實例變量聲明(你想這是每個用戶,所有用戶共享)filePath,然後內doGet()地址:

String filePath = (String) session.getAttribute("filePath"); 

一定要在沒有填充的情況下檢查它是否爲空。