SpringMVC文件上传一直报路径错,求解 10
jsp代码<formaction="${pageContext.request.contextPath}/UpLoadFile/toUpLoadFile"method="...
jsp代码
<form action="${pageContext.request.contextPath }/UpLoadFile/toUpLoadFile" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username" id="username"><br>
文件:<input type="file" name="file" id="file"><br>
<input type="submit" value="提交" >
</form>
control代码
@Resource
@RequestMapping(value="/toUpLoadFile",method=RequestMethod.POST)
public String execute(MultipartFile file,HttpServletRequest req)
throws Exception {//使用MultipartFile 在SpringMVC下 不能直接用File
if (file != null) {
//System.out.println(file.getName()+"--"+file.getSize());
String filename=file.getOriginalFilename();
//SaveFileFromInputStream(file.getInputStream(),req.getRealPath("resources/importfile"),filename);//保存到服务器的路径
//List<StoreEntity> entitys = readFileInfo(req.getRealPath("resources/importfile")+"/"+filename);
SaveFileFromInputStream(file.getInputStream(),req.getRealPath("c:\\struts"),filename);
List<StoreEntity> entitys = readFileInfo(req.getRealPath("c:\\struts")+"/"+filename);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
配置文件
<context:component-scan base-package="control" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/Admin/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
到底有什么问题,求大神 展开
<form action="${pageContext.request.contextPath }/UpLoadFile/toUpLoadFile" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username" id="username"><br>
文件:<input type="file" name="file" id="file"><br>
<input type="submit" value="提交" >
</form>
control代码
@Resource
@RequestMapping(value="/toUpLoadFile",method=RequestMethod.POST)
public String execute(MultipartFile file,HttpServletRequest req)
throws Exception {//使用MultipartFile 在SpringMVC下 不能直接用File
if (file != null) {
//System.out.println(file.getName()+"--"+file.getSize());
String filename=file.getOriginalFilename();
//SaveFileFromInputStream(file.getInputStream(),req.getRealPath("resources/importfile"),filename);//保存到服务器的路径
//List<StoreEntity> entitys = readFileInfo(req.getRealPath("resources/importfile")+"/"+filename);
SaveFileFromInputStream(file.getInputStream(),req.getRealPath("c:\\struts"),filename);
List<StoreEntity> entitys = readFileInfo(req.getRealPath("c:\\struts")+"/"+filename);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
配置文件
<context:component-scan base-package="control" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/Admin/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
到底有什么问题,求大神 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询