我在我的网站上使用Spring MVC 4.
当我使用enctype="multipart/form-data"
进行文件上传时,将无法获取数据,当我删除enctype="multipart/form-data"
时,表单数据将被正确获取,但文件上传将无效。
请指导我为什么会这样。
我的代码如下。
的index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
<p>Hello! This is the default welcome page for a Spring Web MVC project.</p>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file1"><br>
<input type="text" name="nm"><br>
<input type="text" name="age">
<input type="submit" name="sub" value="upload">
</form>
</body>
</html>
的success.jsp
<%--
Document : success
Created on : Mar 1, 2016, 11:19:45 AM
Author : Vishal
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>File uploaded</h1>
</body>
</html>
FileControl.java
package Myc;
import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author sai
*/
@Controller
public class FileControl {
@RequestMapping(value = "/home")
public ModelAndView home() {
ModelAndView mv = new ModelAndView("index");
return mv;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String fileup(HttpServletRequest req, HttpServletResponse res) {
String path = req.getRealPath("/uploadimg");
path = path.substring(0, path.indexOf("\\build"));
path = path + "\\web\\uploadimg\\";
String name=req.getParameter("nm"); //not able to fetch value
String age=req.getParameter("age"); //not able to fetch value
System.out.println(name);
System.out.println(age);
DiskFileItemFactory d = new DiskFileItemFactory();
ServletFileUpload uploader = new ServletFileUpload(d);
try {
List<FileItem> lst = uploader.parseRequest(req);
for (FileItem fileItem : lst) {
if(fileItem.isFormField()==false){
fileItem.write(new File(path+"/"+fileItem.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "success";
}
}
调度-servlet.xml中
<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="Myc"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
答案 0 :(得分:1)
我修复了FileControl
控制器。
首先,您必须使用fileItem
对象来获取参数&#39;值。
我还会添加检查\web\uploading\
是否存在。
<强> FileControl.java 强>
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author sai
*/
@Controller
public class FileControl {
@RequestMapping(value = {"/home","/"})
public ModelAndView home() {
ModelAndView mv = new ModelAndView("index");
return mv;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String fileup(HttpServletRequest req) {
String path = req.getRealPath("/uploadimg");
path = path.substring(0, path.indexOf("\\build"));
path = path + "\\web\\uploading\\";
String name;
String age;
DiskFileItemFactory d = new DiskFileItemFactory();
ServletFileUpload uploader = new ServletFileUpload(d);
try {
List<FileItem> lst = uploader.parseRequest(req);
for (FileItem fileItem : lst) {
if (fileItem.isFormField()) {
if (fileItem.getFieldName().equals("nm")) {
name = fileItem.getString();
System.out.println(name);
} else if (fileItem.getFieldName().equals("age")) {
age = fileItem.getString();
System.out.println(age);
}
} else {
File savingPath = new File(path);
if (!savingPath.exists()) {
savingPath.mkdirs();
}
fileItem.write(new File(path+"/"+fileItem.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return "success";
}
}