文件上传原理


参考 Struts 2.X权威指南(第3版)-李刚   第六章第一节

enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码

application/x-www-form-urlencoded在发送前编码所有字符(默认)
multipart/form-data

不对字符编码。

在使用包含文件上传控件的表单时,必须使用该值。

text/plain空格转换为 "+" 加号,但不对特殊字符编码。

<!DOCTYPE html>
<html>
<head>
    <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
    <title> enctype属性测试 </title>
</head>
<body>
<form id="form1" name="form1" 
    enctype="multipart/form-data"
    method="post" action="upload">
    上传文件:<input type="file" name="file" /><br />  
    请求参数:<input type="text" name="wawa" /><br />
    <input name="dd" type="submit" value="提交" />
</form>
</body>
</html>

uploadpro.jsp文件

<%@ page contentType="text/html;charset=GBK" import="java.io.*"%>
<%
InputStream is = request.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
while( (buffer = br.readLine()) != null)
{
    if(buffer.endsWith("--") && buffer.startsWith("-----------------------------"))
    {
        break;
    }
    if(buffer.startsWith("-----------------------------"))
    {
        if (br.readLine().indexOf("filename") > 1)
        {
            br.readLine();
            br.readLine();
            File file = new File(request.getRealPath("/") + System.currentTimeMillis());
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            String content = null;
            while( (content = br.readLine()) != null)
            {
                if(content.startsWith("-----------------------------"))
                {
                    break;
                }
                ps.println(content);
            }
            ps.flush();
            ps.close();
        }
    }
}
br.close();
%>







转载于:https://my.oschina.net/zhongwenhao/blog/279502

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值