struts图片上传实例
㈠ 怎么用struts2实现多图片上传
新建Web Project,在WebRoot下新建upload文件夹
在WebRoot下新建upload.jsp,上传界面
编写上传成功、失败的提示界面。
在WebRoot下新建uploadError.jsp
在WebRoot下新建 uploadSuccess.jsp
编写Action类
配置struts.xml文件,重置fileUpload拦截器。
测试,测试完成之后在tomcat下面webapps目录下找到项目对应的文件夹下的upload下查看
㈡ struts上传多张图片怎么做,要求可以说上传中文的,而且图片可以重复上传不被覆盖
你指的是一次上传多张吗?我这有个自己写的例子:
//上传实体类
public class Upload {
private File photoes; //封装上传文件的属性
private String photoesFileName; //封装上传文件的名称属性
private String photoesContentType; //封装上传文件的类型属性
private String targetdir; //保存路径
private String targetfilename; //保存的文件名
public File getPhotoes() {
return photoes;
}
public void setPhotoes(File photoes) {
this.photoes = photoes;
}
public String getPhotoesFileName() {
return photoesFileName;
}
public void setPhotoesFileName(String photoesFileName) {
this.photoesFileName = photoesFileName;
}
public String getPhotoesContentType() {
return photoesContentType;
}
public void setPhotoesContentType(String photoesContentType) {
this.photoesContentType = photoesContentType;
}
public String getTargetdir() {
return targetdir;
}
public void setTargetdir(String targetdir) {
this.targetdir = targetdir;
}
public String getTargetfilename() {
return targetfilename;
}
public void setTargetfilename(String targetfilename) {
this.targetfilename = targetfilename;
}
}
这是上传图片的Action类中的部分代码
//获得服务器上保存上传文件的目录images的绝对路径
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//设置保存文件的目录
uld.setTargetdir(realpath);
//设置目标文件名
uld.setTargetfilename(generateFileName(uld.getPhotoesFileName()));
//把doc内容复制到target
FileUtils.File(uld.getPhotoes(), new File(uld.getTargetdir(),uld.getTargetfilename()));
㈢ struts2上传图片
可以直接在上传的时候就创建一个tomcat下的目录吧 图片都放里面
㈣ 在Struts2中如何上传图片并将刚刚上传的图片立即显示出来
//action的关键代码
import com.xt.gyz.facelook.service.FacesService;
public class ShowPic extends ActionSupport {
private InputStream is;
private Integer id;
private FacesService facesService;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void setFacesService(FacesService facesService) {
this.facesService = facesService;
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
@Override
public String execute() throws Exception {
Faces face=facesService.findById(id);
is=new ByteArrayInputStream(face.getPic());
// FileOutputStream fos=new FileOutputStream(new File("E:/body.jpg"));
//顷返 System.out.println(fos);
// System.out.println("face:"+face.getPic().length);
//圆乎消 fos.write(face.getPic());
// fos.flush();
// fos.close();
return super.execute();
}
}
//不要忘记struts.xml配置
......
<action name="showPic" class="showPicAction">
<result type="stream">
<param name="inputName">is</param>
<param name="contectType">image/jpg</橘知param>
</result>
</action>
.....
这样就可以啦