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!

posted @ 2018-06-08 16:23  呦,可以呦  阅读(138)  评论(0编辑  收藏  举报