由于Tomcat对于文件上传的请求做了处理,所有直接getParameter是获取不到的。由于我用的是servlet3.0,所以加了一个注解就可以了。贴代码:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import bean.VideoFile;
import util.Json;
/**
* Servlet implementation class VideoFileUploadServilet
*/
@MultipartConfig() //就是这个注解,让我的servlet可以通过getParameter获得参数
@WebServlet("/VideoFileUploadServlet")
public class VideoFileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public VideoFileUploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
VideoFile file = new VideoFile();
file.input(request);
String statement="mapping.VideoFileMapper.insertFile";
file.setSavePath(util.FileUtil.fileUpload(request, this,"Video"));
System.out.println(file);
SqlSession session=util.DataUtil.getSqlSession();
session.insert(statement,file);
session.commit();
session.close();
Json json = new Json();
System.out.println(file);
json.addValue("isSuccess", file.getSavePath() != null);
PrintWriter pw = response.getWriter();
pw.println(json.getJSONArray());
pw.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}