主题: struts2文件上传,action检测不到文件
作者: qweqwe, 发布日期: 2011-02-20 10:43:34, 浏览数: 6736

struts2文件上传,action检测不到request里有文件,是怎么回事,  System.out.println(itr.hasNext())=false
 请看代码 谢谢

 

FileUpLoad.java

 

public class FileUpLoad extends ActionSupport {

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 
 private String getError(String message) {
  JSONObject obj = new JSONObject();
  obj.put("error", 1);
  obj.put("message", message);
  return obj.toJSONString();
 }
 public void imageUp()  {
  System.out.println("1");
  HttpServletRequest request=ServletActionContext.getRequest();
  HttpServletResponse response=ServletActionContext.getResponse();
  String savePath = request.getSession().getServletContext().getRealPath("/") + "attached/";
  PrintWriter out = null;
  try {
   out = response.getWriter();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
  System.out.println("2");
  //文件保存目录URL
  String saveUrl  = request.getContextPath() + "/attached/";
  //定义允许上传的文件扩展名
  String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
  //最大文件大小
  long maxSize = 1024*500;
  response.setContentType("text/html; charset=UTF-8");
  if(!ServletFileUpload.isMultipartContent(request)){
   out.println(getError("请选择文件。"));
   return;
  }
  //检查目录
  System.out.println("3");
  File uploadDir = new File(savePath);
  if(!uploadDir.isDirectory()){
   out.println(getError("上传目录不存在。"));
   return;
  }
  //检查目录写权限
  System.out.println("4");
  if(!uploadDir.canWrite()){
   out.println(getError("上传目录没有写权限。"));
   return;
  }
  System.out.println("5");
  DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
  upload.setHeaderEncoding("UTF-8");
  List items = null;
  try {
   items = upload.parseRequest(request);
  } catch (FileUploadException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  Iterator itr = items.iterator();
  System.out.println(itr.hasNext());
  while (itr.hasNext()) {
   System.out.println("ok!");
   FileItem item = (FileItem) itr.next();
   String fileName = item.getName();
   long fileSize = item.getSize();
   if (!item.isFormField()) {
    //检查文件大小
    if(item.getSize() > maxSize){
     out.println(getError("上传文件大小超过限制。"));
     return;
    }
    //检查扩展名
    System.out.println("6");
    String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    if(!Arrays.<String>asList(fileTypes).contains(fileExt)){
     out.println(getError("上传文件扩展名是不允许的扩展名。"));
     return;
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
    try{
     System.out.println("7");
     File uploadedFile = new File(savePath, newFileName);
     item.write(uploadedFile);
    }catch(Exception e){
     out.println(getError("上传文件失败。"));
     return;
    }
    JSONObject obj = new JSONObject();
    obj.put("error", 0);
    obj.put("url", saveUrl + newFileName);
    System.out.println("hihihi");
    out.println(obj.toString());
    System.out.println(obj.toString());
   }
  }
 }

}

 

 

 

HTML:

<div id="main" style="text-align:center">
<form id="articleForm"  method="post" >
标题:<s:textfield name="articleTitle" id="articleTitle"  size="50" maxlength="40"></s:textfield>
<br/><br/>
<s:textarea id="content" name="content" cssStyle="width:650px;height:500px;visibility:hidden;"></s:textarea>
<br />
<br />
</form>
<input type="button" id="subArticle" value="点击发布"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="点击清除"/>
</div>

 

 

 

 

发表新帖 发表回复