码迷,mamicode.com
首页 > Web开发 > 详细

servlet 3 文件上传

时间:2014-12-09 17:09:38      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

1.up.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<form action="upload.do" method="post" enctype="multipart/form-data">
content:<input type="text" name="content" value="中国人"><br>
file:<input type="file" name="file1"/><br>
file:<input type="file" name="file2"/><br>
file:<input type="file" name="file3"/><br>
<input type="submit" value="上传文件"/>
</form>

2.Upload.java

package com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@MultipartConfig()
@WebServlet("/upload.do")
public class Upload extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 单文件
        // resp.setContentType("text/html;charset=utf-8");
        // Part part = req.getPart("file");
        // String c = req.getParameter("content");
        // c = new String(c.getBytes("iso8859-1"),"utf-8");
        // resp.getWriter().print(c);
        // String f = part.getHeader("content-disposition");
        // 获取文件名
        // String fn = f.substring(f.lastIndexOf("=")+2,f.length()-1);
        // System.out.println(part.getName());
        // System.out.println(part.getSize());//文件大小
        // System.out.println(part.getContentType());//文件类型
        // part.write(req.getServletContext().getRealPath("upload")+"/"+fn);
        // 多文件
        //for (Part p : req.getParts()) {
        //    if (p.getContentType()!=null) {
        //        String f = p.getHeader("content-disposition");
        //        String fn = f.substring(f.lastIndexOf("=") + 2, f.length() - 1);
        //        p.write(req.getServletContext().getRealPath("upload")+"/"+fn);
        //    }
        //}
    }
}

 

servlet 3 文件上传

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/Dreamlu/p/4153485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!