不允许在spring mvc中加载本地资源

时间:2015-07-25 05:49:16

标签: java spring spring-mvc servlets

在我的spring mvc项目中,我遇到了将图片加载到client side的问题。 console显示不允许加载本地资源 我已经想通了,我需要为观看图像创建图像servlet。 但是当我运行应用程序时没有图像显示..我可以解决这个问题...

servlet在web.xml中配置

<servlet> 
    <servlet-name>studentServlet</servlet-name>
     <servlet-class>com.bbs.response.web.controller.utils.StudentImageServlet</servlet-class>
      </servlet>   
      <servlet-mapping>
       <servlet-name>studentServlet</servlet-name> 
       <url-pattern>/students/</url-pattern>
        </servlet-mapping> 

图片视图servlet

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

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String urlImage = request.getParameter("student"); 
    System.out.println("urlImage---"+urlImage);
        response.setContentType("image/jpeg");
        ServletOutputStream out;
        out = response.getOutputStream();
        FileInputStream fin = new FileInputStream("C:/jboss/welcome-content/students/"+urlImage);

        BufferedInputStream bin = new BufferedInputStream(fin);
        BufferedOutputStream bout = new BufferedOutputStream(out);
        int ch = 0;
        while ((ch = bin.read()) != -1) {
            bout.write(ch);
        }

        bin.close();
        fin.close();
        bout.close();
        out.close();
    }
}

我的用于查看图片的jquery脚本

<img style="height:50px; width:50px" name="student" src="C:/jboss/welcome-content/students/'+ studentBasic.photo+ '" />

0 个答案:

没有答案