1、直接上例子,前台jsp如下,这个例子是多文件上传
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<center>
<font color='red'><s:fielderror></s:fielderror></font>
<form action="${pageContext.request.contextPath}/upload_up.action"
method="post" enctype="multipart/form-data">
<input type="file" name="img" /> <br/>
<input type="file" name="img" /> <br/>
测试普通参数:<input type="text" name="userid"> <br/>
<input type="submit" value="上传"/>
</form>
</center>
</body>
</html>
2、这是action代码
public class UploadAction extends ActionSupport {
private File[] img;
private String userid;
private String[] imgFileName;
private String[] imgContentType;
public File[] getImg() {
return img;
}
public void setImg(File[] img) {
this.img = img;
}
public String[] getImgFileName() {
return imgFileName;
}
public void setImgFileName(String[] imgFileName) {
this.imgFileName = imgFileName;
}
public String[] getImgContentType() {
return imgContentType;
}
public void setImgContentType(String[] imgContentType) {
this.imgContentType = imgContentType;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String up() {
for (int i = 0; i < img.length; i++) {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(img[i]);
String realPath = ServletActionContext.getServletContext()
.getRealPath("/upload")
+ "/";
String fileName = new IPTimeStamp()
.getIPTimeStamp(ServletActionContext.getRequest()
.getRemoteAddr());
fileName += imgFileName[i].substring(imgFileName[i].lastIndexOf("."));
os = new FileOutputStream(new File(realPath + fileName));
// 边读边写
byte[] data = new byte[1024];
int length = 0;
while ((length = is.read(data)) != -1) {
os.write(data, 0, length);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(imgContentType[i]);
}
}
return null;
}
}
多文件上传这里为file数组private File[] img;
而且需要添加这两个属性,private String[] img
FileName;
private String[] img
ContentType;注意属性的名字为xxxFileName和xxxContentType形式,这样就可以通过
imgFileName[i].substring(imgFileName[i].lastIndexOf("."))形式得到后缀名了。
分享到:
相关推荐
使用struts2 上传图片到项目webapps目录下 然后生成缩略图 返回到页面显示缩略图
使用struts2 上传图片,上传图片后生成一张压缩后的图片,和生成一张小图片,在网页上不用显示大图片,显示的缩小后的图片
实现图片的上传和图片的预览,主要用strust2实现的
ckeditor和struts2实现图片上传和编辑器上浏览!
struts2 ajax图片上传
ssh框架用struts2 hibernate实现图片的上传源码
struts2上传图片到Oracle,并进行显示,测试可以成功上传
Struts2+Jquery 同步上传图片
strut 上传图片 网站统计访问量 struts 重复提交
可直接运行,包含jar文件,struts2 图片上传,图片大小,图片格式的限制,客户端文件类型判断等
struts2 批量上传 图片+ 文件 显示进度条
使用struts2框架实现图片上传,不需要js开发
struts2 上传图片限制.struts2 上传图片限制.struts2 上传图片限制.
struts实现图片上传功能。包含详细的代码和配置信息。
Struts2上传图片并生成等比例缩略图的实例,在上传文件的同时生成等比例的缩略图,分放在不同的文件夹下。自己亲测,没有问题。
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2.0图片上传,文件的配置。struts2。0的学习。io的技术。
使用struts框架实现图片上传功能和显示图片。使用struts框架实现图片上传功能和显示图片。