这是我的调度程序代码
@RequestMapping(value="blogWrite", method=RequestMethod.POST)
public ModelAndView writeBlog(BlogText blogText, MultipartFile[] files, HttpSession session) {
System.out.println("writeBlog() 실행");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/Blog/BlogPersonal/blogPersonal");
MemberInfo memberInfo = (MemberInfo)session.getAttribute("session");
final String path = "C:/Users/Dongjoo/Downloads/fileUpload/"+memberInfo.getMemberId();
logger.info("path:{}",path);
blogService.addBlogText(modelAndView,blogText,files,path);
return modelAndView;
}
这是我的服务层代码
@Autowired
private BlogDAO blogDAO;
public MemberInfo getMemberLogin(MemberInfo memberInfo) {
MemberInfo takeMemberInfo = blogDAO.memberLogin(memberInfo);
return takeMemberInfo;
}
public ModelAndView addBlogText(ModelAndView modelAndView, BlogText blogText, MultipartFile[] files, final String path) {
System.out.println("addBlogText() 실행");
for(MultipartFile file : files){
File newFile = new File(path+"/"+file.getOriginalFilename());
try {
FileUtils.writeByteArrayToFile(newFile, file.getBytes());
System.out.println("파일 업로드 성공!");
modelAndView.addObject("message", "파일업로드 성공!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("파일 업로드 실패!");
modelAndView.addObject("message", "파일업로드 실패!");
}
}
return modelAndView;
}
和web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<display-name>CharacterEncodingFilter</display-name>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description></description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
和pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
和writeForm.jsp
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var day = 1;
$('#addBtn').click(function() {
var tagAdd = '';
console.log('일차 : '+day);
if(day==1) {
tagAdd += '<div class="dayAndTimeDiv">';
tagAdd += '<input type="hidden" class="dayRequired" name="dayRequired" value="'+day+'"/>';
tagAdd += '<span class="dayRequiredShow">'+day+'일차</span>';
tagAdd += '<input type="text" class="timeRequired" name="timeRequired" size="2"/>';
tagAdd += '<span class="timeRequiredDiv"> 시간</span>';
tagAdd += '</div>';
tagAdd += '<div class="imageArea">';
tagAdd += '<input type="file" id="blogTextImage" name="blogTextImage"/><br/>';
tagAdd += '<textarea rows="20" cols="50" id="blogTextContent" name="blogTextContent"></textarea>';
tagAdd += '</div>';
$('#writeArea').append(tagAdd);
console.log(tagAdd);
}
else {
/* 지금 태그 추가/삭제가 안 된다. 일단 이것은 나중으로 미루고... */
$('#addBtnDiv').remove();
tagAdd = '<input type="button" id="addBtn"/>';
$('#addBtnDiv').append(tagAdd);
tagAdd = '';
tagAdd += '<div class="dayAndTimeDiv">';
tagAdd += '<input type="hidden" class="dayRequired" name="dayRequired" value="'+day+'"/>';
tagAdd += '<span class="dayRequiredShow">'+day+'일차</span>';
tagAdd += '<input type="text" class="timeRequired" name="timeRequired" size="2"/>';
tagAdd += '<span class="timeRequiredDiv"> 시간</span>';
tagAdd += '</div>';
tagAdd += '<div class="imageArea">';
tagAdd += '<input type="file" id="blogTextImage" name="blogTextImage" multiple="multiple"/><br/>';
tagAdd += '<textarea rows="20" cols="50" id="blogTextContent" name="blogTextContent"></textarea>';
tagAdd += '</div>';
$('#writeArea').append(tagAdd);
console.log(tagAdd);
}
day++;
});
$('#insertBlogTextBtn').click(function() {
$('form').submit();
});
});
</script>
</head>
<body>
<div id="memberNameDiv">
<span id="memberName">${session.memberName}</span>
</div>
<div>
<hr>
</div>
<div id="blogTextArea">
<form action="/blogWrite" method="post" enctype="multipart/form-data">
<div id="categoryDiv">
<span id="categoryHover">카테고리</span>
</div>
<div id="addBtnDiv">
<input type="button" id="addBtn"/>
</div>
<br/>
<div id="writeArea">
</div>
</form>
</div>
<div id="buttonArea">
<input type="button" id="insertBlogTextBtn" name="insertBlogTextBtn" value="글등록"/>
<input type="reset" id="cancelBtn" name="cancelBtn" value="취소"/>
</div>
我没有将DAO部分用于将数据放入数据库
我想放一些文件。
但是我有异常(错误)
下面:
Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.<init>()] with root cause
java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:103)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/wpad.dat] in DispatcherServlet with name 'appServlet'
请帮助我!!!(请忽略韩语我写的内容)
答案 0 :(得分:1)
在serlvet配置中声明CommonsMultipartResolver bean。
例如
For XML config:
--------------
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>2097152</value> <!-- 2MB -->
</property>
</bean>
For java config:
---------------
@Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
In Controller annotation use like :
@RequestParam("photo") MultipartFile file