2014-10-30 36 views
0

我正在創建一個maven項目,並嘗試從我的計算機顯示圖像,但JSP不允許它。 因此#2的搜索後,我得到了一個解決方案是使用FileServlet來處理它, 但嘗試之後,我還是不能讓圖像顯示,所以請幫我在Jsp Spring上從本地驅動顯示圖像

這是我的代碼:

FileServlet的.java

package fileServer; 

import java.io.*; 
import java.net.URLDecoder; 

import javax.servlet.*; 
import javax.servlet.http.*; 

public class FileServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB. 
    private String filePath; 

/** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public FileServlet() { 
    super(); 
    // TODO Auto-generated constructor stub 
    } 

/** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
    *  response) 
    */ 
public void init() throws ServletException { 

    // Define base path somehow. You can define it as init-param of the 
    // servlet. 
    this.filePath = "F:\\DrugStore\\Medicine"; 

    // In a Windows environment with the Applicationserver running on the 
    // c: volume, the above path is exactly the same as "c:\files". 
    // In UNIX, it is just straightforward "/files". 
} 

protected final void doGet(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException, IOException { 

    System.out.println("In do get"); 

    // Get requested file by path info. 
    String requestedFile = request.getPathInfo(); 

    // Check if file is actually supplied to the request URI. 
    if (requestedFile == null) { 
    // Do your thing if the file is not supplied to the request URI. 
    // Throw an exception, or send 404, or show default/warning page, or 
    // just ignore it. 
    response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
    return; 
    } 

    // Decode the file name (might contain spaces and on) and prepare file 
    // object. 
    File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8")); 

    // Check if file actually exists in filesystem. 
    if (!file.exists()) { 
    // Do your thing if the file appears to be non-existing. 
    // Throw an exception, or send 404, or show default/warning page, or 
    // just ignore it. 
    response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. 
    return; 
    } 

    // Get content type by filename. 
    String contentType = getServletContext().getMimeType(file.getName()); 

    // If content type is unknown, then set the default value. 
    // For all content types, see: 
    // http://www.w3schools.com/media/media_mimeref.asp 
    // To add new content types, add new mime-mapping entry in web.xml. 
    if (contentType == null) { 
    contentType = "application/octet-stream"; 
    } 

    // Init servlet response. 
    response.reset(); 
    response.setBufferSize(DEFAULT_BUFFER_SIZE); 
    response.setContentType(contentType); 
    response.setHeader("Content-Length", String.valueOf(file.length())); 
    response.setHeader("Content-Disposition", "attachment; filename=\"" 
      + file.getName() + "\""); 

    // Prepare streams. 
    BufferedInputStream input = null; 
    BufferedOutputStream output = null; 

    try { 
    // Open streams. 
    input = new BufferedInputStream(new FileInputStream(file), 
       DEFAULT_BUFFER_SIZE); 
    output = new BufferedOutputStream(response.getOutputStream(), 
       DEFAULT_BUFFER_SIZE); 

    // Write file contents to response. 
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 
    int length; 
    while ((length = input.read(buffer)) > 0) { 
     output.write(buffer, 0, length); 
    } 
    } finally { 
    // Gently close streams. 
    close(output); 
    close(input); 
    } 
} 

protected final void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
    System.out.println("In do post"); 

} 

private static void close(Closeable resource) { 
    if (resource != null) { 
    try { 
     resource.close(); 
    } catch (IOException e) { 
     // Do your thing with the exception. Print it, log it or mail 
     // it. 
     e.printStackTrace(); 
    } 
    } 
} 
} 

的web.xml

<servlet> 
    <display-name>fileServlet</display-name> 
    <servlet-name>fileServlet</servlet-name> 
    <servlet-class>fileServer.FileServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>fileServlet</servlet-name> 
    <url-pattern>/fileServlet</url-pattern> 
</servlet-mapping> 

的index.jsp

<td colspan="2"><img src="fileServlet?path=${pr.imgname}" /></td> 

感謝幫助

回答

0

我不是很清楚的知道你的代碼是怎麼寫的,但我想所有的JSP頁面都在WEB-INF文件夾,只能通過用SpringMVC請求進行訪問。這是由於春季調度過濾器,爲了讓靜態資源是排除過濾器,可以將靜態文件類型添加到默認的servlet映射如下:

<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.jpg</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>default</servlet-name> 
    <url-pattern>*.bmp</url-pattern> 
</servlet-mapping> 
+0

是flyFox,我所有的JPS頁面在WEB-INF下,感謝您的幫助, – newbier 2014-10-30 07:15:11