난 내가 website.I에서 파일을 다운로드해야 내 스프링 MVC 프로젝트의 일부로서 컨트롤러 방법을 쓴이 :요청 매핑 occued : Not Available 요청한 자원을
//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>
내 디스패처 - 서블릿 :
<?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의 URL 패턴을/to/*로 변경하면 다른 모든 요청이 작동하지 않습니다.
내 질문은 내가 여기서 만든 실수가 무엇입니까? web.xml에서 url 패턴을 변경해야합니까? 그렇다면 어떻게?
편집 :
내가 그것을 .PDF 확장자로 끝나는 URL로 이동합니다 다운로드 링크를 클릭. 그리고 확장자가 인 요청에 대한 핸들러 나 매핑이 없습니다. 이것이 문제가 될 수 있습니까 ??
는이에 대한 자세한 내용을 설명 할 수있다 : "희망이 HTTP에이 페이지가 :/IntelliLabsWeb/oneProfile"당신이 제안 –
ANJU
내가 변경 한 ...하지만 작동하지 않았습니다 – ANJU
작동하지 않았습니까? 여전히 동일한 오류 또는 다른 오류가 발생합니까? –