Struts2文件上传入门简单案例
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>welcome to my home</title> </head> <body> <form action="login.action" method="post" enctype="multipart/form-data"> File:<input type="file" name="file" /><br /> <input type="submit" value="submit" /> </form> </body> </html>
enctype="multipart/form-data"该属性必须填写!
1 public String execute() throws IOException { 2 InputStream inputStream = new FileInputStream(file); 3 File savedFile = new File("C:\\Users\\Administrator\\Desktop\\a.txt"); 4 OutputStream outputStream = new FileOutputStream(savedFile); 5 byte[] b = new byte[1024]; 6 int len = 0; 7 while((len=inputStream.read(b))>0) { 8 outputStream.write(b, 0, len); 9 } 10 inputStream.close(); 11 outputStream.close(); 12 return "success"; 13 }
文件传输需要输入/输出流
简单记忆:------>输入=========【文件】=========输出-------->
输入流读取文件,输出流写入文件
最后关闭流!
OK!
凝一眸碧水,拈一缕清风,于一怀静谧中倾听凡尘的落音。
不再奢求什么,做简单的自己,过简单的生活,心在,梦在,你在,便是光阴赐予我的最美。