struts2 图片处理 即获得服务器路径等

struts2 图片处理  即获得服务器路径等

 

 
//图片处理   保存
		if(!imgContentType.startsWith("image")){
			ActionContext.getContext().put("msg1", "图片格式不正确");
			return "success";
		}
		String[] t=imgFileName.split("\\.");
		String extensionName=t[t.length-1];
		
		//获取服务器的路径    
		// 创建文件夹
		// 创建文件 根据时间(long类型)创建
		HttpServletRequest hsr=ServletActionContext.getRequest();
		String realpath=hsr.getServletContext().getRealPath("/");
		
		File localfile=new File(realpath, "\\img");
		localfile.mkdirs();  //创建服务器的路径
		String newfilename=System.currentTimeMillis()+"."+extensionName;
		localfile=new File(localfile, newfilename);
		try {
			localfile.createNewFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			FileUtils.copyFile(img, localfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	private File img;
	 private String imgContentType; // 文件的内容类型
	    private String imgFileName; // 上传文件名

jsp 页面 enctype一定要设置

	
<form action="house_save" method="post" enctype="multipart/form-data">

<select name="state">
  <option value ="1">在售</option>
  <option value ="2">已售出</option>
  <option value="3">出租</option>
</select><br>
<select name="type">
  <option value ="1">住宅新房</option>
  <option value ="2">住宅二手房</option>
  <option value="3">写字楼</option>
  <option value="4">商业楼</option>
</select><br>

<input type="text" name="add" value="地址"  size="40"/><Br>
<input type="text" name="u_price"  size="40" value="单价"/><Br>
<input type="text" name="area"  size="40" value="面积"/><Br>
<input type="text" name="price"  size="40" value="金额"/>要求数字,非数字为0<Br>
<input type="text" name="description"  size="20" value="信息"/><Br>
<input type="file" name="img"/><Br>
<input type="submit"  value="保存房产信息">
</form>


 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值