Struts2文件上传

<form action="${pageContext.request.contextPath}/TestContextAcion_UploadFile.action" method="post" enctype="multipart/form-data" >
    	上传文件:<input type="file" name="PicImage" value="选择文件">
    	<input type="submit" value="上传">
</form>


// 文件上传
private File PicImage;// 上传的文件
private String PicImageFileName;//上传文件的名称+Filename为固定写法
public String UploadFile() throws Exception {
		if (PicImage != null) {
			String realpath = ServletActionContext.getServletContext().getRealPath("/Images");//绝对路径
			File savefile = new File(new File(realpath), PicImageFileName);
			// 判断保存的路径是否存在,不存在则创建
			if (savefile.getParentFile().exists()) {
				savefile.getParentFile().mkdirs();// 创建目录
			}
			FileUtils.copyFile(PicImage, savefile);// PicImage为源文件,savefile为需要保存到的位置
		}
		return "success";
	}


你可能感兴趣的:(上传文件,null,private,public)