2017-09-25 61 views
0

我有我的春天MVC项目的一部分在那里我必须从website.I下载文件写的控制方法为:请求映射occued:不可用请求的资源

//download resume 
//---------------------------------------------------------------------- 
@RequestMapping(value="/download/{fileName.+}",method=RequestMethod.GET) 
public void downloadPDFResource(HttpServletRequest request, 
     HttpServletResponse response,@PathVariable("fileName") String fileName){ 


    System.out.println("filename i got :"+fileName); 
    //If user is not authorized - he should be thrown out from here itself 

    //String fileName="Ente Kadha - Madhavikkutty.pdf"; 

    //Authorized user will download the file 
    String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/downloads/"); 
    Path file = Paths.get(dataDirectory, fileName); 
    if (Files.exists(file)) { 
     response.setContentType("application/pdf"); 
     response.addHeader("Content-Disposition", "attachment; filename=" + fileName); 
     try { 
      Files.copy(file, response.getOutputStream()); 
      response.getOutputStream().flush(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
    else 
    { 
     System.out.println("\n\nFile not found!!\n\n"); 
     System.out.println(file); 
    } 
} 

视图从那里请求下载的文件页云:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Profile</title> 
    </head> 
    <body> 

     <table> 

      <th><h3>${profile.name}</h3></th> 
     <tr> 
      <td>Application Id :</td> 
      <td>${profile.a_id}</td> 
     </tr> 
     <tr> 
      <td>Name :</td> 
      <td>${profile.name}</td> 
     </tr> 
     <tr> 
      <td>Address :</td> 
      <td>${profile.address}</td> 
     </tr> 
     <tr> 
      <td>Email:</td> 
      <td>${profile.email}</td> 
     </tr> 
     <tr> 
      <td>Phone:</td> 
      <td>${profile.phone}</td> 
     </tr> 
     <tr> 
      <td>Vacancy id:</td> 
      <td></td> 
     </tr> 
     <tr> 
      <td>Date Applied :</td> 
      <td>${profile.dateApplied}</td> 
     </tr> 
     <tr> 
      <td>Resume : ${profile.resumePath}${profile.resumeDoc} </td> 
      <td><a href="download/${profile.resumeDoc}">Download ${profile.resumeDoc}</a></td> 
    </tr> 

</table> 

</body> 
</html> 

我的调度员的servlet:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:p="http://www.springframework.org/schema/p"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:component-scan base-package="controller"></context:component-scan> 
    <context:component-scan base-package="DAO"></context:component-scan> 

    <!--<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>--> 

    <!-- 
    Most controllers will use the ControllerClassNameHandlerMapping above, but 
    for the index controller we are using ParameterizableViewController, so we must 
    define an explicit mapping for it. 
    --> 
    <!--<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="index.htm">indexController</prop> 
      </props> 
     </property> 
    </bean>--> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 

    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 

    <bean id="con" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
     <property name="url" value="//my url"></property> 
     <property name="username" value="root"></property> 
     <property name="password" value="root"></property> 

     <!-- 
     //for ms sql server,it may be like : 
     database-driver=net.sourceforge.jtds.jdbc.Driver 
     url=jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS 
     username=shoppingcart 
     password=12345 
     -->  
    </bean> 

    <!-- normal jdbc template bean--> 
    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="con"></property> 
    </bean> 


    <!-- this bean uses normal jdbc template --> 
    <bean id="contactsDAO" class="DAO.ContactsDAO"> 
     <property name="jdbc" ref="template"></property> 
    </bean> 

    <!-- this bean uses normal jdbc template --> 
    <bean id="vacancyDAO" class="DAO.VacancyDAO"> 
     <property name="jdbc1" ref="template"></property> 
    </bean> 

    <!-- this bean uses normal jdbc template --> 
    <bean id="careerDAO" class="DAO.CareerDAO"> 
     <property name="jdbc2" ref="template"></property> 
    </bean> 

</beans> 

web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

下载链接点击时,我得到了错误:“请求的资源不可用”:

WARNING [http-nio-8084-exec-129] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/IntelliLabsWeb/oneProfile/download/Ente Kadha - Madhavikkutty.pdf] in DispatcherServlet with name 'dispatcher' 

我想有一个错误的映射request.when我更改web.xml中从/到/ *,所有其他请求不起作用。

我的问题是,我在这里犯了什么错误?我应该更改web.xml中的url模式吗?如果是的话,怎么样?

编辑:

,当我点击它进入的.pdf 扩展,结尾的URL下载链接。并且没有处理程序或映射请求与 这样的扩展名。这可能是一个问题?

回答

0

我认为你可以尝试以下2个步骤使其工作。

1)改变请求映射(有文件名之后的小错字,将其删除)

@RequestMapping(value="/download/{fileName}",method=RequestMethod.GET) 

2)改变的联系,(与根斜线)

<a href="/download/${profile.resumeDoc}">Download ${profile.resumeDoc}</a> 

另有,路径名将从应用程序上下文派生。希望你有

http:<server url>/IntelliLabsWeb/oneProfile 

此页面的web.xml配置应该像

<url-pattern>/</url-pattern>

,并希望这应该解决您的问题。

+0

你能解释一下这个:“我希望你有 HTTP此页:/IntelliLabsWeb/oneProfile” – ANJU

+0

我所做的更改如你所说...但没有工作 – ANJU

+0

没有工作,你仍然得到相同的错误或不同的错误? –

0

我是新来的春天框架。 我想你应该修改web.xml文件:

<servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.*</url-pattern> </servlet-mapping>

希望能有所帮助!

+0

你为什么说应该像上面提到的那样修改url模式?实际上,我无法访问任何没有url模式的控制器,如:/ – ANJU

+0

因为\ *。\ *可以映射调度程序servlet的所有请求 –