struts2 上传多个文件的问题
功能要求可以动态的添加,删除上传框,每个框可以上传多个文件。请问action中怎么获得对应上传框中的文件数组,文件名数组和文件类型数组?...
功能要求可以动态的添加,删除上传框,每个框可以上传多个文件。请问action中怎么获得对应上传框中的文件数组,文件名数组和文件类型数组?
展开
2个回答
2013-09-11
展开全部
获得对应上传框中的文件数组,文件名数组和文件类型数组这个容易,你看下面代码就知道了import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.Action;public class uploadfile implements Action,ServletRequestAware {
HttpServletRequest request;
private File[] file;
private String[] fileContentType;
private String[] fileFileName;
HttpServletRequest request;
HttpSession session; public void setServletRequest(HttpServletRequest request)
{this.request=request;}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}*/ public String execute() throws Exception
{
session=request.getSession();
ConBean conbean=new ConBean();
conbean.con(); //连接数据库
String name=(String)session.getAttribute("name");
for(int i=0;i<file.length;i++)
{
String address="resource/"+getFileFileName()[i];
String time=new Date().toString();
String sql="insert into resource(name,s_name,s_address,s_time) values('"+name+"','"+getFileFileName()[i]+"','"+address+"','"+time+"')";
conbean.insert(sql);
FileOutputStream w=new FileOutputStream("../webapps/webDemo/resource/"+getFileFileName()[i]);
FileInputStream r= new FileInputStream(getFile()[i]);
byte[] bt=new byte[19999999];
int len=0;
if((len=r.read(bt))>0)
{
w.write(bt, 0, len);
}
w.close();
r.close();
}
return SUCCESS;}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.Action;public class uploadfile implements Action,ServletRequestAware {
HttpServletRequest request;
private File[] file;
private String[] fileContentType;
private String[] fileFileName;
HttpServletRequest request;
HttpSession session; public void setServletRequest(HttpServletRequest request)
{this.request=request;}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}*/ public String execute() throws Exception
{
session=request.getSession();
ConBean conbean=new ConBean();
conbean.con(); //连接数据库
String name=(String)session.getAttribute("name");
for(int i=0;i<file.length;i++)
{
String address="resource/"+getFileFileName()[i];
String time=new Date().toString();
String sql="insert into resource(name,s_name,s_address,s_time) values('"+name+"','"+getFileFileName()[i]+"','"+address+"','"+time+"')";
conbean.insert(sql);
FileOutputStream w=new FileOutputStream("../webapps/webDemo/resource/"+getFileFileName()[i]);
FileInputStream r= new FileInputStream(getFile()[i]);
byte[] bt=new byte[19999999];
int len=0;
if((len=r.read(bt))>0)
{
w.write(bt, 0, len);
}
w.close();
r.close();
}
return SUCCESS;}
2013-09-11
展开全部
有点想qq空间的上传功能吧
要用到flash比较复制
要用到flash比较复制
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询