此前,书写汽车俱乐部需求分析书时,我对会员基本信息中人头像的切换,没有一个比较好的思路去实现。现在终于有了点头绪,部分代码如下:
另外,下午注意到一种情况,如果传入的图片大小不符合,或者用户自定义截图(仿似CSDN),那么实现起来就会复杂一些。这种效果看起来挺cool......
function showing(){
var x = document.getElementById("imgFile");
if(!x || !x.value)
{
return;
}
var path = /\.jpg$|\.jpeg$|\.gif$/i;
if(path.test(x.value))
{
var img=f1.imgFile.value;
f1.img_name.src=img;
}
else
{
alert("格式不正确");
return false;
}
}
<html:form action="/sales?mySales=fileUp" styleId="f1" enctype="multipart/form-data" onsubmit="return showing();">
<div align="center">
公司标志:<img border=0 src="../images/gtxin/logo.jpg" width="140" height="87" id="img_name" />
<html:file property="filecontext" onclick="showing();" styleId="imgFile"/>
<input type="submit" value="上 传" class="common_button"/><html:errors property="filecontext" />
</div>
</html:form>
private FormFile filecontext;//setter() and getter
FormFile upfile=fileupForm.getFilecontext();
//建立文件夹
/* File dir=new File("D:\\"+msg);
if(!dir.isDirectory()) dir.mkdir();*/
try {
byte[] buf=upfile.getFileData();//获取文件的内容
// InputStream in=upfile.getInputStream();
OutputStream out=new FileOutputStream(new File("c:\\"+imgCount+".jpg"));
out.write(buf);
out.close();
System.out.println(upfile.getFileSize());
System.out.println(upfile.getFileName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
另外,下午注意到一种情况,如果传入的图片大小不符合,或者用户自定义截图(仿似CSDN),那么实现起来就会复杂一些。这种效果看起来挺cool......