struts2 单个文件上传的三种方法以及多文件上传
2011-10-25 19:42
483 查看
<!--[if !supportLists]-->1、
<!--[endif]-->填加JAR包:commons-fileupload-1.2.1.jar
、connons-io-1.3.2.jar放在WEB-INF/lib下
2、在from表单增加enctype属性<form action=”” method=””
enctype=”multipart/form-data”
3、Struts.xml配置文件中引入上传文件的配置
<include file=”cn/csdn/struts2/xml/file/struts-upload.xml”></include>
4、Struts-upload.xml配置文件中填加上传的action处理.并且初始化上传文件路径参数
<!-- 上传文件action的处理 -->
Xml代码
<action name="uploadfile" class="cn.csdn.action.GoodsAction2" method="uploadfile">
<!-- 初始化文件保存路径参数为/uploads -->
<param name="savePath">/uploads</param>
<result name="success">/ac.jsp</result>
</action>
以上是准备工作 ,做好之后就是上传功能的实现
第一种方法:用字节流实现
核心代码:
Java代码
private String savePath;
private String title;
private File pic;// 文件名 与视图层的名称一致
private String picContentType;//文件名+ContentType
private String picFileName;//文件名+FileName
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
//文件上传功能实现:方法一:字节流
public String uploadfile(){
System.out.println("getSavePath()"+getSavePath());
System.out.println("savePath:"+savePath);
System.out.println("title:"+title);
System.out.println("pic:"+pic);
System.out.println("picContentType:"+picContentType);
System.out.println("picFileName:"+picFileName);
FileInputStream fis=null;
FileOutputStream fos=null;
//定义保存的路径
String savepath=getSavePath();
//根据路径创建文件路径对象
File file=new File(savepath);
if(!file.exists()){
file.mkdirs();
}
try {
//创建输入流
fis=new FileInputStream(pic);
//创建输出流
fos=new FileOutputStream(savepath+"//"+picFileName);
byte buf[]=new byte[1024];
int n=0;
while((n=fis.read(buf))!=-1){
fos.write(buf, 0, n);
}
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
第二种方法:用FileUtils
核心代码:
Java代码
// 文件上传功能实现:方法二:FileUtils public String uploadfile() { // 定义保存的路径 String savepath = getSavePath(); // 根据路径创建文件路径对象 File file = new File(savepath); if (!file.exists()) { file.mkdirs(); } try { FileUtils.copyFile(pic, new File(file, getPicFileName())); } catch (Exception ex) { ex.printStackTrace(); } return SUCCESS; }
第三种方法:三层管道
核心代码:
Java代码
// 文件上传功能实现:方法三:三层管道
public String uploadfile() {
BufferedReader br = null;
BufferedWriter bw = null;
// 定义保存的路径
String savepath = getSavePath();
// 根据路径创建文件路径对象
File file = new File(savepath);
if (!file.exists()) {
file.mkdirs();
}
try {
// 创建输入流
br = new BufferedReader(new InputStreamReader(new FileInputStream(
pic)));
// 创建输出流
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file + "//" + getPicFileName())));
char buf[] = new char[1024];
int n = 0;
while ((n = br.read(buf)) != -1) {
bw.write(buf, 0, n);
}
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
三种比较 ,笔者认为还是第二种比较简单。
会了单文件上传,多文件上传就简单了。只需要把获得文件、文件类型、文件名称 定义声明时定义为数组的就可以了
核心代码:
Java代码
private String savePath;
private String title;
private File[] pic;// 文件名 与视图层的名称一致
private String[] picContentType;// 文件名+ContentType
private String[] picFileName;// 文件名+FileName
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getPic() {
return pic;
}
public void setPic(File[] pic) {
this.pic = pic;
}
public String[] getPicContentType() {
return picContentType;
}
public void setPicContentType(String[] picContentType) {
this.picContentType = picContentType;
}
public String[] getPicFileName() {
return picFileName;
}
public void setPicFileName(String[] picFileName) {
this.picFileName = picFileName;
}
// 文件上传功能实现:多文件上传
public String uploadfiles() {
// 定义保存的路径
String savepath = getSavePath();
// 根据路径创建文件路径对象
File file = new File(savepath);
if (!file.exists()) {
file.mkdirs();
}
try {
for(int i=0;i<pic.length;i++){
FileUtils.copyFile(pic[i], new File(file, getPicFileName()[i]));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return SUCCESS;
}
}
<!--[endif]-->填加JAR包:commons-fileupload-1.2.1.jar
、connons-io-1.3.2.jar放在WEB-INF/lib下
2、在from表单增加enctype属性<form action=”” method=””
enctype=”multipart/form-data”
3、Struts.xml配置文件中引入上传文件的配置
<include file=”cn/csdn/struts2/xml/file/struts-upload.xml”></include>
4、Struts-upload.xml配置文件中填加上传的action处理.并且初始化上传文件路径参数
<!-- 上传文件action的处理 -->
Xml代码
<action name="uploadfile" class="cn.csdn.action.GoodsAction2" method="uploadfile">
<!-- 初始化文件保存路径参数为/uploads -->
<param name="savePath">/uploads</param>
<result name="success">/ac.jsp</result>
</action>
<action name="uploadfile" class="cn.csdn.action.GoodsAction2" method="uploadfile"> <!-- 初始化文件保存路径参数为/uploads --> <param name="savePath">/uploads</param> <result name="success">/ac.jsp</result> </action>
以上是准备工作 ,做好之后就是上传功能的实现
第一种方法:用字节流实现
核心代码:
Java代码
private String savePath;
private String title;
private File pic;// 文件名 与视图层的名称一致
private String picContentType;//文件名+ContentType
private String picFileName;//文件名+FileName
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
//文件上传功能实现:方法一:字节流
public String uploadfile(){
System.out.println("getSavePath()"+getSavePath());
System.out.println("savePath:"+savePath);
System.out.println("title:"+title);
System.out.println("pic:"+pic);
System.out.println("picContentType:"+picContentType);
System.out.println("picFileName:"+picFileName);
FileInputStream fis=null;
FileOutputStream fos=null;
//定义保存的路径
String savepath=getSavePath();
//根据路径创建文件路径对象
File file=new File(savepath);
if(!file.exists()){
file.mkdirs();
}
try {
//创建输入流
fis=new FileInputStream(pic);
//创建输出流
fos=new FileOutputStream(savepath+"//"+picFileName);
byte buf[]=new byte[1024];
int n=0;
while((n=fis.read(buf))!=-1){
fos.write(buf, 0, n);
}
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
private String savePath; private String title; private File pic;// 文件名 与视图层的名称一致 private String picContentType;//文件名+ContentType private String picFileName;//文件名+FileName public String getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getPic() { return pic; } public void setPic(File pic) { this.pic = pic; } public String getPicContentType() { return picContentType; } public void setPicContentType(String picContentType) { this.picContentType = picContentType; } public String getPicFileName() { return picFileName; } public void setPicFileName(String picFileName) { this.picFileName = picFileName; } //文件上传功能实现:方法一:字节流 public String uploadfile(){ System.out.println("getSavePath()"+getSavePath()); System.out.println("savePath:"+savePath); System.out.println("title:"+title); System.out.println("pic:"+pic); System.out.println("picContentType:"+picContentType); System.out.println("picFileName:"+picFileName); FileInputStream fis=null; FileOutputStream fos=null; //定义保存的路径 String savepath=getSavePath(); //根据路径创建文件路径对象 File file=new File(savepath); if(!file.exists()){ file.mkdirs(); } try { //创建输入流 fis=new FileInputStream(pic); //创建输出流 fos=new FileOutputStream(savepath+"//"+picFileName); byte buf[]=new byte[1024]; int n=0; while((n=fis.read(buf))!=-1){ fos.write(buf, 0, n); } if(fis!=null){ fis.close(); } if(fos!=null){ fos.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; }
第二种方法:用FileUtils
核心代码:
Java代码
// 文件上传功能实现:方法二:FileUtils public String uploadfile() { // 定义保存的路径 String savepath = getSavePath(); // 根据路径创建文件路径对象 File file = new File(savepath); if (!file.exists()) { file.mkdirs(); } try { FileUtils.copyFile(pic, new File(file, getPicFileName())); } catch (Exception ex) { ex.printStackTrace(); } return SUCCESS; }
// 文件上传功能实现:方法二:FileUtils public String uploadfile() { // 定义保存的路径 String savepath = getSavePath(); // 根据路径创建文件路径对象 File file = new File(savepath); if (!file.exists()) { file.mkdirs(); } try { FileUtils.copyFile(pic, new File(file, getPicFileName())); } catch (Exception ex) { ex.printStackTrace(); } return SUCCESS; }
第三种方法:三层管道
核心代码:
Java代码
// 文件上传功能实现:方法三:三层管道
public String uploadfile() {
BufferedReader br = null;
BufferedWriter bw = null;
// 定义保存的路径
String savepath = getSavePath();
// 根据路径创建文件路径对象
File file = new File(savepath);
if (!file.exists()) {
file.mkdirs();
}
try {
// 创建输入流
br = new BufferedReader(new InputStreamReader(new FileInputStream(
pic)));
// 创建输出流
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file + "//" + getPicFileName())));
char buf[] = new char[1024];
int n = 0;
while ((n = br.read(buf)) != -1) {
bw.write(buf, 0, n);
}
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
// 文件上传功能实现:方法三:三层管道 public String uploadfile() { BufferedReader br = null; BufferedWriter bw = null; // 定义保存的路径 String savepath = getSavePath(); // 根据路径创建文件路径对象 File file = new File(savepath); if (!file.exists()) { file.mkdirs(); } try { // 创建输入流 br = new BufferedReader(new InputStreamReader(new FileInputStream( pic))); // 创建输出流 bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file + "//" + getPicFileName()))); char buf[] = new char[1024]; int n = 0; while ((n = br.read(buf)) != -1) { bw.write(buf, 0, n); } if (br != null) { br.close(); } if (bw != null) { bw.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; }
三种比较 ,笔者认为还是第二种比较简单。
会了单文件上传,多文件上传就简单了。只需要把获得文件、文件类型、文件名称 定义声明时定义为数组的就可以了
核心代码:
Java代码
private String savePath;
private String title;
private File[] pic;// 文件名 与视图层的名称一致
private String[] picContentType;// 文件名+ContentType
private String[] picFileName;// 文件名+FileName
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getPic() {
return pic;
}
public void setPic(File[] pic) {
this.pic = pic;
}
public String[] getPicContentType() {
return picContentType;
}
public void setPicContentType(String[] picContentType) {
this.picContentType = picContentType;
}
public String[] getPicFileName() {
return picFileName;
}
public void setPicFileName(String[] picFileName) {
this.picFileName = picFileName;
}
// 文件上传功能实现:多文件上传
public String uploadfiles() {
// 定义保存的路径
String savepath = getSavePath();
// 根据路径创建文件路径对象
File file = new File(savepath);
if (!file.exists()) {
file.mkdirs();
}
try {
for(int i=0;i<pic.length;i++){
FileUtils.copyFile(pic[i], new File(file, getPicFileName()[i]));
}
} catch (Exception ex) {
ex.printStackTrace();
}
return SUCCESS;
}
}
相关文章推荐
- struts2中文件上传文件大小限制以及解决方法(common-FileUpload)
- struts2中文件上传文件大小限制以及解决方法(common-FileUpload)
- PHP 多文件上传的三种方法
- HTML5 FormData 方法介绍以及实现文件上传
- struts2之单个文件上传
- 在php.ini中设置表单上传文件的大小,以及获取设置的值的方法 .
- [推荐]GAE 应用文件上传的三种方法
- Struts2之单个文件上传
- fckedit和struts2集成不能上传文件的解决方法
- struts2之单个文件上传
- 用.NET判断上传文件类型的三种方法,最后一种真正意义上的判断
- springMVC高级部分(数据校验,数据错误回显(自定义格式错误显示),拦截器,异常处理,文件上传,文件下载,springmvc运行流程以及springmvc和struts2对比)
- 使用Struts2上传文件超过2M报错解决方法
- springMVC文件上传的三种方法
- struts2文件上传的三种方式解析
- ASP.NET上传文件的三种基本方法
- Struts2文件上传以及文件名中文乱码解决
- struts2实现单个和多个文件的上传
- struts2文件上传,看到书上讲的action类的执行方法不是execute而是upload,不解,故搜之
- mysql导入导出csv文件语句,以及建表时候如何设置主键的三种方法