上传到一个文件夹下
在struts2.0下
private File upload;//上传文件域内容,比如图片
private String uploadFileName;//上传文件名,图片的名字,get,set方法
File file=new File(相对路径,名字)
FileUtils.copyFile(upload, file);
jsp页面有文件上传这个域和普通的文字域
调试,如果带有图片不报错,不带图片老是报空指针异常,也就是下面的“名字”那个地方
File file=new File(相对路径,名字)

解决方案 »

  1.   

    private File file;

    private String fileFileName;

    private String fileContextType; @SuppressWarnings("deprecation")
    @Override
    public String execute() throws Exception {

    InputStream is = new FileInputStream(file);

    String path = ServletActionContext.getRequest().getRealPath("/upload");

    File myfile = new File(path,this.fileFileName);

    OutputStream os = new FileOutputStream(myfile);

    byte[] buffer =new byte[400];

    int length = 0;

    while((length = is.read(buffer)) > 0){

    os.write(buffer,0,length);
    }
    os.close();
    is.close();
    return SUCCESS;
    }
      

  2.   

    private File upload;//上传文件域对象
    private String uploadFileName;//上传文件名
    public File getUpload() {
    return upload;
    }
    public void setUpload(File upload) {
    this.upload = upload;
    }
    public String getUploadFileName() {
    return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }
    @Override
    public String execute() throws Exception {
                    String dstPath=ServletActionContext.getServletContext().getRealPath("/UserImage");
                    File dstFile=new File(dstPath,uploadFileName);
    FileUtils.copyFile(upload, dstFile);
            VO.setFile(图片名字);
            DAO.insertUser(urvo);
            return success
    }
    需要在D:\tomcat-6.0.20\webapps\dmg下建一个文件夹UserImagestruts.xml配置
    <struts>

    <package name="struts-job" extends="struts-default">
    <action name="UserRegisterAction" class="com.dmg.action.UserRegisterAction">
    <param name="savePath">/upload</param>
    <result name="success" type="redirectAction">UserRegisterAction!getUserById</result>
    </action>
    </package>
    </struts>
      

  3.   

    File myfile = new File(path,this.fileFileName); 试过了,如果没有图片会报空指针异常,有责正常,就是没用到输入输出流,图片能上传到建的那个文件夹下
      

  4.   

    你没图片,File找不到文件当然会有错没看懂什么意思,我晕
      

  5.   

    在哪判断,set,get,还是在 File dstFile=new File(dstPath,uploadFileName);这之前判断