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>