servlet3.0 getPart()与getParts()获取上传文件
2018-02-06 00:00
555 查看
需求: servlet3.0以上,就是tomcat7.0以上,web.xml->web-app->version 3.0以上, 不需要引入其它的jar包
Servlet类需要添加注释@MultipartConfig, 必要, 否则request.getPart()会为null
[java] view plain copy
@MultipartConfig
public class FileServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//文件存放目录
String mypath = this.getClass().getClassLoader().getResource("/../../").getPath() + "upload";
File file = new File(mypath );
if(!file.exists()){
file.mkdir();
}
String filename = format.format(new Date());
// multipart/form-data
if (ServletFileUpload.isMultipartContent(request)) {
Part part = request.getPart("ff");
if(part == null){
return;
}
String headerfNValue = part.getHeader("content-disposition");
if("".equals(headerfNValue.trim()){
return ;
}
//headerfNValue = [filename="xxxx.xxx"]
String valueKey = "filename=\"";
int s = headerfNValue.indexOf(valueKey );
if(s < 0){
return;
}
String originalfilename = headerfNValue.substring(s + valueKey.length(), headerfNValue.length()-1);
;
String suffix = originalfilename.substring(originalfilename.lastIndexOf("."));
if(".jpg|.jpeg|.jpe|.jfif|.png|.gif|.bmp|.dib|.tif|.tiff".indexOf(suffix) < 0){
return;
}
filename += suffix;
InputStream input = part.getInputStream();
file = new File(mypath + filename);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
int size = 0;
byte[] buffer = new byte[1024];
while ((size = input.read(buffer)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
input.close();
}
}
}
getParts() 就是多个Part ,for出来就可以
关于 @MultipartConfig 注释:
@MultipartConfig(location = "/upload", maxFileSize = 1024 * 1024 * 50)
设置了location 就是文件的保存路径 , 确认Part的filename不为空后,可以直接写入, 可以省去后面的文件流操作
......
filename += suffix;
part.write(filename);
前端页面:
<form action="" method ="post" enctype="multipart/form-data">
<input type="file" name="ff" />
<input type="submit" value="go" />
</form>
Servlet类需要添加注释@MultipartConfig, 必要, 否则request.getPart()会为null
[java] view plain copy
@MultipartConfig
public class FileServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//文件存放目录
String mypath = this.getClass().getClassLoader().getResource("/../../").getPath() + "upload";
File file = new File(mypath );
if(!file.exists()){
file.mkdir();
}
String filename = format.format(new Date());
// multipart/form-data
if (ServletFileUpload.isMultipartContent(request)) {
Part part = request.getPart("ff");
if(part == null){
return;
}
String headerfNValue = part.getHeader("content-disposition");
if("".equals(headerfNValue.trim()){
return ;
}
//headerfNValue = [filename="xxxx.xxx"]
String valueKey = "filename=\"";
int s = headerfNValue.indexOf(valueKey );
if(s < 0){
return;
}
String originalfilename = headerfNValue.substring(s + valueKey.length(), headerfNValue.length()-1);
;
String suffix = originalfilename.substring(originalfilename.lastIndexOf("."));
if(".jpg|.jpeg|.jpe|.jfif|.png|.gif|.bmp|.dib|.tif|.tiff".indexOf(suffix) < 0){
return;
}
filename += suffix;
InputStream input = part.getInputStream();
file = new File(mypath + filename);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
int size = 0;
byte[] buffer = new byte[1024];
while ((size = input.read(buffer)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
input.close();
}
}
}
getParts() 就是多个Part ,for出来就可以
关于 @MultipartConfig 注释:
@MultipartConfig(location = "/upload", maxFileSize = 1024 * 1024 * 50)
设置了location 就是文件的保存路径 , 确认Part的filename不为空后,可以直接写入, 可以省去后面的文件流操作
......
filename += suffix;
part.write(filename);
前端页面:
<form action="" method ="post" enctype="multipart/form-data">
<input type="file" name="ff" />
<input type="submit" value="go" />
</form>
相关文章推荐
- servlet3.0 getPart()与getParts( 4000 )获取上传文件
- servlet3.0 Part接口实现form表单文件上传
- 用servlet实现文件上传报错java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getPart
- @MultipartConfig,Servlet3.0通过Part进行文件上传
- servlet3.0 part上传文件
- 在Servlet3.0中使用request.getParts()获取上传文件
- servlet3.0新特性测试,文件上传(1)
- SpringMVC4.2.4 基于原生Servlet 3.0 文件上传
- Servlet 知识点(三) -- Servlet 的文件上传功能的表单获取问题(2)
- Servlet3.0中使用getPart进行文件上传
- servlet 3.0 文件上传下载
- Servlet 3.0笔记之超方便的文件上传支持
- Java乔晓松-Struts2中文件上传和多文件上传以及获取HttpServletRequest / HttpSession / ServletContext / HttpServletRespon
- Servlet 3.0 的文件上传功能
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
- servlet3.0上传文件
- Servlet3.0学习总结——基于Servlet3.0的文件上传
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
- Servlet 3.0新特性——文件上传接口
- JavaWeb开发知识总结(annotation,Servlet3.0,文件上传,动态代理)