Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jsp Spring上显示来自本地驱动程序的图像_Spring_Jsp - Fatal编程技术网

在Jsp Spring上显示来自本地驱动程序的图像

在Jsp Spring上显示来自本地驱动程序的图像,spring,jsp,Spring,Jsp,我正在创建一个maven项目,我尝试从我的计算机显示图像,但JSP不允许这样做。 所以在搜索Stackoverflow之后,我得到了一个解决方案,就是使用FileServlet来处理它, 但在尝试之后,我仍然无法得到图像显示,所以请帮助我 这是我的代码: FileServlet.java package fileServer; import java.io.*; import java.net.URLDecoder; import javax.servlet.*; import javax.

我正在创建一个maven项目,我尝试从我的计算机显示图像,但JSP不允许这样做。 所以在搜索Stackoverflow之后,我得到了一个解决方案,就是使用FileServlet来处理它, 但在尝试之后,我仍然无法得到图像显示,所以请帮助我

这是我的代码:

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>

文件服务器
文件服务器
fileServer.FileServlet
文件服务器
/文件服务器
index.jsp

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


感谢您的帮助

我不清楚您的代码是如何编写的,但我猜您的所有jsp页面都在WEB-INF文件夹中,只能通过springMVC请求访问。这是由于spring dispatcher筛选器,以便将静态资源排除在筛选器之外,您可以将静态文件类型添加到默认servlet映射,如下所示:


违约
*jpg先生
违约
*.bmp

是的,flyFox,我所有的jps页面都在WEB-INF下,谢谢您的帮助,