文件上传struts2 实现文件上传功能(1)

(一)从底层透析文件上传的实现,此时并没有介入struts2
1、upload.jsp,在form中属性method默认为get,涉及文件上传时必须改为post,默认enctype="application/x-www-form-urlencoded" ,我们暂且不修改,看会有什么结果

 1 <% @ page language="java" contentType="text/html; charset=GBK"
 2    pageEncoding="GBK"
%>
 3 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
 4 < html >
 5      < head >
 6          < meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
 7          < title > Insert title here </ title >
 8      </ head >
 9      < body >
10          < form  action ="result.jsp"  method ="post"
11             enctype ="application/x-www-form-urlencoded" >
12             Information:
13              < input  type ="text"  name ="info" >
14              < br >
15             File:
16              < input  type ="file"  name ="file" >
17              < br >
18              < input  type ="submit"  name ="submit"  value =" submit " >
19          </ form >
20      </ body >
21 </ html >
result.jsp
 1 <% @ page language="java" contentType="text/html; charset=GBK"
 2    pageEncoding="GBK" %>
 3 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
 4 < html >
 5      < head >
 6          < meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
 7          < title > Insert title here </ title >
 8      </ head >
 9      < body >
10         Information: <% = request.getParameter( " info " ) %> < br >
11         File: <% = request.getParameter( " file " ) %> < br >
12      </ body >
13 </ html >
结果:



2、修改result.jsp页面代码,输出读入的流
<% @ page import="java.io.*" %>
     < body >
        
<%
            InputStream 
is = request.getInputStream();
            BufferedReader br 
= new BufferedReader(new InputStreamReader(is));
            
String buffer = null;
            
while ((buffer = br.readLine()) != null) {
                out.print(buffer 
+ "<br>");
            }
        
%>
    
</ body >
结果:


这个结果可以断定,文件的上传并没有成功,而仅仅是上传了文件的路径信息而已

3、把upload.jsp中form的enctype属性改为 enctype ="multipart/form-data"
         < form  action ="result.jsp"  method ="post"  enctype ="multipart/form-data" >
            Information:
            
< input  type ="text"  name ="info" >
            
< br >
            File:
            
< input  type ="file"  name ="file" >
            
< br >
            
< input  type ="submit"  name ="submit"  value =" submit " >
        
</ form >
结果:
 

说明文件上传是成功的。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值