<!--[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的处理 -->
- <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>
以上是准备工作 ,做好之后就是上传功能的实现
第一种方法:用字节流实现
核心代码:
- 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
核心代码:
- // 文件上传功能实现:方法二: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;
}
第三种方法:三层管道
核心代码:
- // 文件上传功能实现:方法三:三层管道
- 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;
}
三种比较 ,笔者认为还是第二种比较简单。
会了单文件上传,多文件上传就简单了。只需要把获得文件、文件类型、文件名称 定义声明时定义为数组的就可以了
核心代码:
- 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;
- }
- }