FormFie类取不到,form和action一起的特点,使我找不到,解决的方法?

解决方案 »

  1.   

    用apache的fileupload实现文件上传比较好,网上资料也很多
      

  2.   

    知道这个 但是我的框架 没有多个action actionForm 正常的去做,我没成功 .
      

  3.   

    我用的是jpetstore的框架写的,和struts的传统框架有所不同 ,在jsp页面提交以后,直接提交到action,action和actionform是在一起的,这也是有别于struts框架的地方,所以不能用一个formbean类取到表单中的form。jsp页面简单的form提交:
    <form method="post" name="proRefForm" action="saveForm.shtml" ><table id ="uploadFile" border = 0 cellspacing=0 cellpadding =0 >
     <tr>
    <td align = "left">

    <input type="file" id="file_0" name="file[0]" style = "width:455"   onchange="insertNextFile();" align = "left" >
      
     </td>
    </tr>
    </table>
    <form>
    直接提交到bean中,这个框架的bean中是把actionform和action放到了一起,所以不能像struts正常的框架先取actionform:String path=request.getSession().getServletContext().getRealPath(""); UploadService uploadService=new UploadService(request, path+"\\common\\Upload", path+"\\common\\Upload\\TEMP");
    uploadService.upLoad();用的是commons fileupload组件,提醒我request中没有(the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null)package com.ibatis.bugcentral.service;/**
     * 处理文件的上传
     */
    public class UploadService {

    private String tempPath;    // 文件上传的临时存放路径
    private String filePath;    // 文件的实际存放路径
    private int sizeThreshold = 4096; // 允许存放在内存中的最大字节数
    private long sizeMax = -1; // 允许用户上串的最大字节数 -1为没有限制 private HttpServletRequest uploadReq=ActionContext.getActionContext().getRequest(); // 文件上传请求 private ServletFileUpload fileUpload;
    private boolean pathError = false; // 文件路径错误或创建时发生错误
    private String[] fileName; // 存储的文件的文件名(可以多个上传)
    private int fileCount;


    /*
     * 构造方法
     */

    public UploadService(HttpServletRequest req, String filePath, String tempPath) {
    this.uploadReq = req;
    this.filePath = filePath;
    this.tempPath = tempPath;
    init();
    } public UploadService(HttpServletRequest req, String filePath, String tempPath,
    long sizeMax) {
    this.uploadReq = req;
    this.filePath = filePath;
    this.tempPath = tempPath;
    this.sizeMax = sizeMax;
    init();
    } public UploadService(HttpServletRequest req, String filePath, String tempPath,
    long sizeMax, int sizeThreshold) {
    this.uploadReq = req;
    this.filePath = filePath;
    this.tempPath = tempPath;
    this.sizeMax = sizeMax;
    this.sizeThreshold = sizeThreshold;
    init();
    } /*
     * 初始化方法
     */
    private void init() {
    DiskFileItemFactory dfileFac = new DiskFileItemFactory();
    dfileFac.setRepository(new File(tempPath));
    dfileFac.setSizeThreshold(sizeThreshold); fileUpload = new ServletFileUpload(dfileFac);
    fileUpload.setSizeMax(sizeMax); // 检查指定的文件夹是否存在,不存在则创建
    File myfile = null;
    try {
    myfile = new File(filePath);
    if (!myfile.isDirectory()) {
    myfile.mkdirs();
    }
    myfile = null;
    myfile = new File(tempPath);
    if (!myfile.isDirectory()) {
    myfile.mkdirs();
    }
    myfile = null;
    } catch (SecurityException ex) {
    ex.printStackTrace();
    pathError = true;
    } try {
    fileCount=fileUpload.parseRequest(uploadReq).size();
    } catch (FileUploadException e) {
    e.printStackTrace();
    }
    } /**
     * 处理文件上传操作,默认是一个文件
     * 
     * @return
     */
    public boolean upLoad() {
    return upLoad(fileCount);
    } /**
     * 返回第一个文件的文件名,对应处理一个文件上传时
     * 
     * @return
     */
    public String getFileName() {
    return fileName[0];
    } /*
     * 处理文件上传操作重构方法 fileNums 为文件上传的个数
     */
    public boolean upLoad(int fileNums) {
    // 文件路径错误时候返回false
    if (pathError) {
    return false;
    } Iterator it;
    // 解析请求
    try {
    fileUpload.setHeaderEncoding("UTF-8");
    List fileItems = fileUpload.parseRequest(uploadReq);
    it = fileItems.iterator(); } catch (FileUploadException ex) {
    ex.printStackTrace();
    return false;
    }
    for(int i=0;i<fileCount;i++){
    // 处理每个item
    fileName = new String[fileNums];
    int icount = i;
    while (it.hasNext()) {
    FileItem fileItem = (FileItem) it.next();
    if (!fileItem.isFormField()) { // 忽略其他不是文件域的表单域
    String[] temp = fileItem.getName().split(
    File.separator + File.separator);
    // fileName[icount]=temp[temp.length-1];
    fileName[icount] = "cp_" + System.currentTimeMillis() + "_"
    + temp[temp.length - 1];
    long size = fileItem.getSize();
    if ((fileName[icount] == null || fileName[icount].equals(""))
    && size == 0) {
    continue;
    }
    try {
    File file = new File(filePath + File.separator
    + fileName[icount]);
    if (file.exists()) { // 当文件存在时先删除再保存
    file.delete();
    file = null;
    }
    fileItem.write(new File(filePath + File.separator
    + fileName[icount])); // 写文件到指定的目录
    } catch (Exception ex) {
    ex.printStackTrace();
    return false;
    }
    ++icount;
    }
    }

    }
    return true;
    } /**
     * 取所有上传的文件名
     * 
     * @return
     */
    public String[] getFileNames() {
    return fileName;
    }

    /**
     * 下载
     * 
     * @return
     */
    public void downLoad(HttpServletRequest request,HttpServletResponse response,String filename){

    try {
    String path=request.getSession().getServletContext().getRealPath("");

    response.setHeader("Content-disposition","attachment; filename="+filename); FileInputStream fis=new FileInputStream(path+"\\common\\upload"+filename);  OutputStream os=response.getOutputStream();  int byteRead;  while(-1 != (byteRead = fis.read())) {  os.write(byteRead);  }  os.close();  if (fis != null)  fis.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    }

        
    }
    主要问题就是取不到request中提交的问价上传的数据,所以无法完成文件上传的功能。
      

  4.   

    要在页面上的form表单加上enctype="MULTIPART/FORM-DATA"
    如果还是没请求,ActionBean 和forBean断开,因为forBean有一个方法拦截你的上传请求