upload.jsp: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="upload2" method="post" enctype="multipart/form-data"> 上传相片:<input type="file" name="photo"/><br><br> <input type="submit" value="上传" name="upload"/> </form> </body> </html> upload2.java: package picture; 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(location="E:/COURSE/") @WebServlet("/upload2") public class upload2 extends HttpServlet { private static final long serialVersionUID = 1L; public upload2() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); Part part=request.getPart("photo"); String filename=getFilename(part); part.write(filename); } private String getFilename(Part part) { String header=part.getHeader("Content-Disposition"); String filename=header.substring(header.indexOf("filename=\"")+10, header.lastIndexOf("\"")); return filename; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
求指点
慕九州4223353
相关分类