struts2文件上传

1 public String upload() {
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(file+"//"+getPicFileName());
byte b[] = new byte[1024];
int n=0;
while((n=fis.read(b))!=-1){
fos.write(b, 0, n);
}
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
2        public String upload() {
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 b[] = new char[1024];
int n=0;
while((n=br.read(b))!=-1){
bw.write(b, 0, n);
}
if(br!=null){
br.close();
}
if(bw!=null){
bw.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
3 public String upload() {
FileInputStream fis = null;
FileOutputStream fos = null;

String savePath = getSavepath();

File file = new File(savePath);

if (!file.exists()) {
file.mkdirs();
}

try {
FileUtils.copyFile(pic,new File(file,getPicFileName()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}

你可能感兴趣的:(struts2)