2017-09-25 64 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的所有請求 –