struts2文件上传,upload

在后台文件中:

private File image;   //文件

private String imageFileName;   //文件名称

public String upload(){

    String realPath=ServletActionContext.getServletContext().getRealPath("/imag");

    File file=new File(realPath);

    if(!file.exists()){

    file.mkdirs();

}

try {

    FileUtils.copyFile(image, new File(file,imageFileName));   //copy到imag文件中

} catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

    return "success";

}

public File getImage() {

    return image;

}

public void setImage(File image) {

    this.image = image;

}

public String getImageFileName() {

    return imageFileName;

}

public void setImageFileName(String imageFileName) {

    this.imageFileName = imageFileName;

}

jsp页面中:

<form action="user/user_upload.action" method="post" enctype="multipart/form-data"> 

    <input type="file" name="image" border="1"/> <input type="submit" value="上传"/>

 </form>

这样就可以实现简单的文件上传了!

猜你喜欢

转载自myokm123.iteye.com/blog/1837262