天天看点

struts2实现文件上传代码方法

public String getImages(File file,String tu) throws Exception {

System.out.println("pictureFileName:"+file.getName()+"tu:"+tu);

File saved = new File(ServletActionContext.getServletContext().getRealPath("images"),imgsmallFileName);

InputStream ins=null;

OutputStream ous=null;

try {

saved.getParentFile().mkdirs();

ins=new FileInputStream(file);

ous=new FileOutputStream(saved);

byte[] b=new byte[1024];

int len=0;

while((len=ins.read(b))!=-1){

ous.write(b, 0, len);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

if(ous!=null){

ous.close();

}

if (ins!=null) {

ins.close();

}

}

return imgsmallFileName;

}