码迷,mamicode.com
首页 > Web开发 > 详细

Struts2文件上传

时间:2015-05-26 16:37:42      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:private   上传文件   public   null   

<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";
	}


Struts2文件上传

标签:private   上传文件   public   null   

原文地址:http://cblovey.blog.51cto.com/10052824/1655097

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!