前一种方法只能上传一些小的文件.
因为写入的长度为文件的长度,假设有一个文件有 2 G 
而你电脑的内存只有 1 G 你说你的这个文件怎么上传啊.
后一种每次读 8192 k 就可以麻.

解决方案 »

  1.   

    从来不用循环的办法。我的实现如下:public class New_chanpinAction extends BaseAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    New_chanpinForm new_chanpinForm = (New_chanpinForm) form;// TODO FormFile file = new_chanpinForm.getImg_url(); if (("".equals(new_chanpinForm.getImg_url().getFileName())
    || (new_chanpinForm.getImg_url().getFileName() == null))) {
    ActionErrors errors = new ActionErrors();
    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
    "chanpin_img_url_null"));
    this.saveErrors(request, errors);
    return mapping.getInputForward();
    }

    if (!((file.getContentType().equals("image/bmp"))
    || (file.getContentType().equals("image/gif"))
    || (file.getContentType().equals("image/jpg")) || (file
    .getContentType().equals("image/jpeg")))) {
    ActionErrors errors = new ActionErrors();
    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
    "file_type_wrong"));
    this.saveErrors(request, errors);
    return mapping.getInputForward();
    } this.getChanpin_service().updateFile(request, file);////////////////****************** Chanpin chanpin = new Chanpin();
    chanpin.setImgUrl("UserFiles/Image/" + file.getFileName());
    chanpin.setJianjie(new_chanpinForm.getJianjie().trim());
    chanpin.setMingcheng(new_chanpinForm.getMingcheng().trim()); this.getChanpin_service().save(chanpin); return mapping.findForward("list_chanpin"); }
    }
    updateFile方法实现如下: public void updateFile(HttpServletRequest request, FormFile formfile) {
    FileOutputStream fileOutput;
    try {
    fileOutput = new FileOutputStream(request
    .getRealPath("/UserFiles/Image")
    + "/" + formfile.getFileName());
    fileOutput.write(formfile.getFileData());
    fileOutput.flush();
    fileOutput.close();
    } catch (Exception e) {
    e.printStackTrace();
    } }
      

  2.   

    楼上的,saveErrors在新的struts里面已经不能用了吧?
    好像要改成saveMessages