servlet3.0中提供了对文件上传的直接支持,不需要借助任何第三方上传组件,直接使用Servlet3.0提供的API就能够实现文件上传功能。
servlet 代码:
package ni.jun.yang.servlet; import java.io.File; import java.io.IOException; import java.util.UUID; 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; @WebServlet("/Upload") @MultipartConfig //必须使用此注解,标志此servlet 支持文件上传 public class Upload extends HttpServlet { private static final long serialVersionUID = 1L; public Upload() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置请求编码 request.setCharacterEncoding("utf-8"); //(part)此类表示在multipart/form-dataPOST请求中收到的部分或表单项 。 // Part part = request.getPart("filename"); //获取上下文对象,再获取某文件夹的位置 String path = request.getServletContext().getRealPath("file"); //获得头部信息 String headName = part.getHeader("content-disposition");
System.out.println(headName); //截取文件后缀名 String end = headName.substring(headName.indexOf("."), headName.length()-1); //随机生成一个文件名,并拼接完整路径 String filename = path +File.separator+UUID.randomUUID().toString()+end; System.out.println(filename); //将上传的项目写入磁盘的方便方法。 part.write(filename); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
part接口:http://docs.oracle.com/javaee/6/api/javax/servlet/http/Part.html
html代码:文件上传一定要将enctype的值改为:enctype="multipart/form-data"
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form action="Upload" method="post" enctype="multipart/form-data"> <input type="file" name="filename"><br/><br/> <input type="submit" value="上传"> </form> </body> </html>
请求头信息:
效果:
下载:
package ni.jun.yang.servlet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/DownServlet") public class downServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取要下载的文件的绝对路径 String realPath = this.getServletContext().getRealPath("111.txt"); System.out.println(realPath); //2.获取要下载的文件名 String fileName = realPath.substring(realPath.lastIndexOf(File.separator)+1); String userAgent = request.getHeader("User-Agent"); //针对IE或者以IE为内核的浏览器: if (userAgent.contains("MSIE")||userAgent.contains("Trident")) { fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); } else { //非IE浏览器的处理: fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); } //3.设置content-disposition响应头控制浏览器以下载的方式打开文件 response.setHeader("content-disposition","attachment;filename="+fileName); //4.获取要下载的文件输入流 InputStream in = new FileInputStream(realPath); int len = 0; //5.创建书缓冲区 byte[] buffer = new byte[1024]; //6.通过response对象获取OutputStream输出流对象 OutputStream os = response.getOutputStream(); //7.将FileInputStream流对象写入到buffer缓冲区 while((len=in.read(buffer))>0){ os.write(buffer,0,len); } //8.关闭流 in.close(); os.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }