首页 > 代码库 > servlet 3 文件上传

servlet 3 文件上传

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 文件上传