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;
import java.io.IOException;
import java.io.InputStream;
@WebServlet("/testServlet")
@MultipartConfig(maxFileSize = 5*1024*1024)
public class fileUpload extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 其他数据还可以跟之前一样正常读,当然,也可以用req.getPart读
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
// 读取文件
Part file = req.getPart("file");
System.out.println(file.getName());
// 输出文件内容
InputStream inputStream = file.getInputStream();
byte[] buf = new byte[1024];
int n = inputStream.read(buf);
String s = new String(buf,0,n,"utf-8");
System.out.println(s);
}
}