项目发布 就是有emctype=""的jsp提交到action中无法用req.getParameter()等到普通控件 解决:琢磨了一天,退而求其次,放弃ssh框架,用了普通的springmvc
package com.njit.action;
public class ProjectUpload extends HttpServlet
{
private static final long serialVersionUID = 1325954832925856683L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// SetCharacterEncoding = UTF-8
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=UTF-8");
String savePath = getServletContext().getRealPath("/upload");
File saveDir = new File(savePath);
// 如果目录不存在,就创建目录
if(!saveDir.exists()){
saveDir.mkdir();
}
// Judge multipart or not
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
// Multipart form
if (isMultipart)
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
// Parse the request
List<FileItem> items = upload.parseRequest(req);
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
// Parameters map
Map<String,String> params = new HashMap<String,String>();
// Do list
while (iter.hasNext())
{
FileItem item = iter.next();
// Form Field
if (item.isFormField())
{
// Field name
String name = item.getFieldName();
// Set charset = UTF-8 Default = ISO-8859-1
// Get field value
String value = item.getString("utf-8");
// Put into map
params.put(name, value.trim());
} else{
String fileName = item.getName();
//设置不允许上传的文件格式
if(fileName.endsWith(".exe")){
req.setAttribute("msg", "不允许上传的类型!");
}else{
//将文件保存到指定的路径
File file = new File(savePath,fileName);
item.write(file);
}
}
}
String name = (String)params.get("name");
String brief = (String)params.get("brief");
String category = (String)params.get("category");
....................
ClassPathXmlApplicationContext resource = new
ClassPathXmlApplicationContext("applicationContext.xml");
ProjectService projectService=(ProjectService)resource.getBean("projectServiceTarget");
Project pro=new Project();
pro.setName(name);
pro.setAuthority(authority);
.......
projectService.add(pro);
}
catch (Exception fue)
{
fue.printStackTrace();
}
}
}
}
posted on 2016-12-06 17:42 YanXingFire 阅读(145) 评论(0) 编辑 收藏 举报