struts2 下传单个文件的注意要点
struts2 上传单个文件的注意要点:
1. struts上传文件,推荐文件名为英文或者数字组成。
本人用中文测试了一下上传图片,图片一直显示不了。
2 控制台输出"[Removing file userImg D:\Program Files\apache-tomcat-6.0.26\work\Catalina\localhost\Struts2_upload\upload__62f8d30b_12fce6fb31b__8000_00000001.tmp[/color]"
其实是删除临时文件夹里面的接收到的临时文件,这个这是作为一个日志提示,不碍事。真正的图片已被上传到项目里 你指定的一个文件夹里面。
如:Action类里面部分代码:
private File userImg;
private String userImgContentType;
private String userImgFileName;
public String execute()throws Exception{
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getUserImgFileName());
FileInputStream fis=new FileInputStream(getUserImg());
FileOutputStream fos=new FileOutputStream(destFile);
byte[] buffer=new byte[1024];
int len=0;
System.out.println("test1");
while ((len=fis.read(buffer))>0) {
System.out.println("test");
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
return SUCCESS;
}
跳转到视图层显示图片的代码:
<img src="upload/<s:property value='userImgFileName'/>"/>
3 若要对上传文件格式进行控制,可在设置参数;
如: <param name="allowedTypes">
image/gif,
image/png,
image/jpeg,<!--火狐浏览器上传jpg格式的图片需要的参数-->
image/pjpeg<!--ie,360浏览器上传jpg格式的图片需要的参数-->
</param>
1. struts上传文件,推荐文件名为英文或者数字组成。
本人用中文测试了一下上传图片,图片一直显示不了。
2 控制台输出"[Removing file userImg D:\Program Files\apache-tomcat-6.0.26\work\Catalina\localhost\Struts2_upload\upload__62f8d30b_12fce6fb31b__8000_00000001.tmp[/color]"
其实是删除临时文件夹里面的接收到的临时文件,这个这是作为一个日志提示,不碍事。真正的图片已被上传到项目里 你指定的一个文件夹里面。
如:Action类里面部分代码:
private File userImg;
private String userImgContentType;
private String userImgFileName;
public String execute()throws Exception{
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root,this.getUserImgFileName());
FileInputStream fis=new FileInputStream(getUserImg());
FileOutputStream fos=new FileOutputStream(destFile);
byte[] buffer=new byte[1024];
int len=0;
System.out.println("test1");
while ((len=fis.read(buffer))>0) {
System.out.println("test");
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
return SUCCESS;
}
跳转到视图层显示图片的代码:
<img src="upload/<s:property value='userImgFileName'/>"/>
3 若要对上传文件格式进行控制,可在设置参数;
如: <param name="allowedTypes">
image/gif,
image/png,
image/jpeg,<!--火狐浏览器上传jpg格式的图片需要的参数-->
image/pjpeg<!--ie,360浏览器上传jpg格式的图片需要的参数-->
</param>