项目需求上传图片
由于之前项目都是用spring mvc框架 用MultipartFile来上传图片
今日遇到struts2 框架还棘手了一把 ,不过不的说strut2 上传文件真的是麻烦
首先配置xml
struts.xml 配置上传文件
<constant name="struts.multipart.saveDir" value="C:/InfoPath/tmp"></constant>// 此处路径是临时文件存放
// struts 上传文件 生成的是临时文件 需要获取这个临时文件夹中的临时文件在通过流的方式写入到 你要存的盘符中
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="action"></constant>
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.custom.i18n.resources" value="app,message"></constant>
<constant name="struts.multipart.maxSize" value="1000485760"/>
代码
首先是表单
重点是name 接下来再看 后台代码
<label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图1:</label>
<div>
<input type="file" maxlength="500" id="imgFile1" name="pic1" οnchange="getPhotoSize(this)"/>
<span style="padding:0 0 0 5px;"><b class="redColor">*</b></span>
</div>
</div>
<div class="form-group">
<label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图2:</label>
<div>
<input type="file" maxlength="500" id="imgFile2" name="pic2" οnchange="getPhotoSize(this)"/>
</div>
</div>
<div class="form-group">
<label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图3:</label>
<div>
<input type="file" maxlength="500" id="imgFile3" name="pic3" οnchange="getPhotoSize(this)"/>
</div>
</div>
<div class="form-group">
<label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图4:</label>
<div>
<input type="file" maxlength="500" id="imgFile4" name="pic4" οnchange="getPhotoSize(this)" />
</div>
</div>
后台代码
// 此处变量名 和表单中 name 对应起来
private File pic1;
private File pic2;
private File pic3;
private File pic4;
// xxxxFileName 固定格式
private String pic1FileName;
private String pic2FileName;
private String pic3FileName;
private String pic4FileName;
// 文件类型 可不写
private String pic1ContentType;
// 当然以上变量都需要setget方法 不多说
一下才是真正上传文件的代码
if (pic1 != null && !"".equals(pic1)) {
fileName1 = WebUtils.getRandomId()+ pic1FileName.substring(pic1FileName.length() - 4,pic1FileName.length());
// root 为路径
String result = uploadFile(root, pic1, fileName1);
if (result.equals("success")) {
String image1 = contextPath + Constant.img + fileName1;
// 删除原图
if (organizations.getPic_url()!=null&&!"".equals(organizations.getPic_url())) {
String str = organizations.getPic_url().substring(organizations.getPic_url().lastIndexOf("/"));
String path = realPath+str;
File oldfile = new File(path);
oldfile.delete();
}
organizations.setPic_url(image1);
pic1 = null;
}
public String uploadFile(String root ,File thumbanil,String fileName) throws IOException{
// 上传文件方法
FileInputStream fiptS = new FileInputStream(thumbanil);
InputStream iptS = (InputStream) fiptS;
FileOutputStream foptS = new FileOutputStream(new File(root, fileName));
OutputStream optS = (OutputStream) foptS;
int c;
while((c=iptS.read())!=-1)
{
optS.write(c);
}
optS.flush();
optS.close();
return "success";
}