Servlet实现文件上传
来源:互联网 发布:linux route 修改 编辑:程序博客网 时间:2024/05/22 12:20
Servlet3.0开始新增了Part接口,专门用来处理文件上传。
package javax.servlet.http;import java.io.IOException;import java.io.InputStream;import java.util.Collection;public interface Part { public InputStream getInputStream() throws IOException; public String getContentType();//获得内容属性如image/png public String getName();//获得input name属性值与request.getPart参数值一致 public String getSubmittedFileName();//获得上传文件名 public long getSize(); public void write(String fileName) throws IOException;//将上传内容写入文件 public void delete() throws IOException; public String getHeader(String name);//根据标头名称返回标头信息 public Collection<String> getHeaders(String name);//根据标头名称返回标头信息集合 public Collection<String> getHeaderNames();//返回标头名称集合}
比较重要的几个方法:
/** * Obtain the content type passed by the browser. * * @return The content type passed by the browser or <code>null</code> if * not defined. */public String getContentType();获得文件类型信息,如image/jpeg;image/png。
/** * If this part represents an uploaded file, gets the file name submitted * in the upload. Returns {@code null} if no file name is available or if * this part is not a file upload. * * @return the submitted file name or {@code null}. * * @since Servlet 3.1 */public String getSubmittedFileName();获得上传文件名称,如test.jpg
/** * A convenience method to write an uploaded part to disk. The client code * is not concerned with whether or not the part is stored in memory, or on * disk in a temporary location. They just want to write the uploaded part * to a file. * * This method is not guaranteed to succeed if called more than once for * the same part. This allows a particular implementation to use, for * example, file renaming, where possible, rather than copying all of the * underlying data, thus gaining a significant performance benefit. * * @param fileName The location into which the uploaded part should be * stored. Relative locations are relative to {@link * javax.servlet.MultipartConfigElement#getLocation()} * * @throws IOException if an I/O occurs while attempting to write the part */public void write(String fileName) throws IOException;上传文件。
/** * Obtain the size of this part. * * @return The size of the part if bytes */public long getSize();获得上传文件大小(字节)。
Demo:
upload.html
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>upload</title></head><body> <form action="upload.do" method="post" enctype="multipart/form-data"> 上传图片:<input type="file" name="photo" value="" /> <br /> <input type="submit" value="上传" name="upload" /> </form></body></html>uploadServlet.java
import javax.servlet.ServletException;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.PrintWriter;import java.time.LocalDate;import java.time.LocalTime;import java.time.format.DateTimeFormatter;//import javax.servlet.annotation.MultipartConfig;//import javax.servlet.annotation.WebServlet;/** * Created by N3verL4nd on 2017/1/9. *///@MultipartConfig(location = "D:/JAVA/IdeaProjects/JavaProj/out/artifacts/jspRun_war_exploded/upload")//@WebServlet(name = "uploadServlet", urlPatterns = {"/upload.do"})public class uploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Part part = request.getPart("photo"); //获得上传文件名称(实际过程中应该重命名自己想要的格式) String filename = part.getSubmittedFileName(); //类型检查如:只要图片格式文件 if (part.getContentType().startsWith("image")) { String finalFileName = getPreFileName() + filename.substring(filename.indexOf('.', 0), filename.length()); //写入文件 //part.write(finalFileName); out.println("<h2>" + finalFileName + " 上传成功</h2>"); } else { out.println("<h2>上传失败</h2>"); } //返回input标签name值(与request.getPart参数一致) //out.println(part.getName()); /*//返回标头名称集合(如content-disposition;content-type) for (String str : part.getHeaderNames()){ out.println(str + "<br />"); }*/ /*//根据标头名称返回标头详细信息(如form-data; name="photo"; filename="ing.png") for (String str : part.getHeaders("content-disposition")){ out.println(str + "<br />"); }*/ /* //返回内容类型 out.println(part.getContentType() + "<br />"); //返回标头信息 out.println(part.getHeader("content-disposition") + "<br />"); //返回上传文件大小 out.println(part.getSize() + " 字节"); //返回上传文件名 out.println(part.getSubmittedFileName()); */ //out.println(this.getServletContext().getRealPath("upload")); } //随机生成文件名 private String getPreFileName(){ StringBuilder stringBuilder = new StringBuilder(); LocalDate date = LocalDate.now(); //格式化日期字符串 stringBuilder.append(date.format(DateTimeFormatter.BASIC_ISO_DATE)); LocalTime time = LocalTime.now().withNano(0); //格式化时间字符串 stringBuilder.append(time.format(DateTimeFormatter.ofPattern("HHmmss"))); return stringBuilder.toString(); }}
上传服务器文件名如:20170109214600.png
关于MultipartConfig:
@MultipartConfig
该注解主要是为了辅助 Servlet 3.0 中 HttpServletRequest 提供的对上传文件的支持。该注解标注在 Servlet 上面,以表示该 Servlet 希望处理的请求的 MIME 类型是 multipart/form-data。另外,它还提供了若干属性用于简化对上传文件的处理。具体如下:
表 5. @MultipartConfig 的常用属性
0 0
- Servlet实现文件上传
- Servlet实现文件上传
- Servlet实现文件上传
- servlet实现文件上传
- servlet实现文件上传
- servlet实现文件上传
- Servlet实现文件上传
- Servlet实现文件上传
- servlet实现文件上传
- servlet实现文件上传
- servlet实现文件上传
- servlet实现文件上传
- servlet实现文件上传
- Servlet实现文件上传
- servlet实现文件上传
- Servlet实现文件上传
- servlet实现文件上传
- Servlet实现文件上传
- Debian 8 x86_64 Java 使用 NRSerialPort 开发串口通信
- Apache HTTP Server 2.2.31在Windows下安装和简单配置
- Linux curl命令学习
- Git整理与秘技
- MTK串口通信调试
- Servlet实现文件上传
- 浏览器打开pdf乱码
- 关于IOS UINavigationBar 界面切换颜色变换的问题
- 用freeDiameter封装Diameter消息
- c# 添加ssk皮肤
- 无题
- 使用Vuforia和Unity开发AR小游戏(一)
- SQL资料总结
- 421. Maximum XOR of Two Numbers in an Array