注意 jsp页面中file选择 的要有属性 name='file'
package com.webserver.webservice;

import java.io.File;
import java.io.IOException;
import java.util.List; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; import org.apache.catalina.core.ApplicationPart; /**
* Servlet implementation class UpLoadServlet
* author:chuanyuBai 2014/07/21
*/
@MultipartConfig(location = "D:/upload", fileSizeThreshold = 1024)
public class PCMUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public PCMUploadServlet() {
super();
} @Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");
try{
//获取多文件
List<Part> partlist = (List<Part>) request.getParts();
//获取单文件文件部件part
Part part = request.getPart("file");
//获取文件名称的两种方式
//方式一
String filename1 = ((ApplicationPart)part).getFilename();
//方式二
//获取文件服务器头部信息
String h = part.getHeader("content-disposition");
String filename = h.substring(h.lastIndexOf("=") + 2, h.length() - 1);
String root = request.getServletContext().getRealPath("/upload");
File f=new File(root);
f.mkdir();
//删除生成的中间文件
part.delete();
part.write(root+"/"+filename);
}catch(Exception e){
System.out.println(e.getMessage());
} } }

最新文章

  1. SQL Server 2008登录错误:无法连接到(local)解决方法
  2. LintCode First Bad Version
  3. Linux socket编程应用学习笔记
  4. 获取txt文件指定行内容
  5. Pike学习笔记
  6. Apple Pay强势来袭,开发者应做的事情
  7. 修改css
  8. 下载cppunit
  9. 常用排序算法之——选择排序(C语言+VC6.0平台)
  10. Linux中环境变量到底写在哪个文件中?解析login shell 和 no-login shell
  11. 进入TP-Link路由器之后利用快捷键F12查看星号路由密码的方法
  12. JSONP ---------跨域
  13. Java多线程2:线程的使用及其生命周期
  14. 听大佬学长RQY报告有感
  15. asp.net core2.0中网站发布的时候,怎么样才配置才可以使视图文件不被打包进去?
  16. iOS.Notification.Bar.Color
  17. [.NET]解决EMF图像自动放大空白
  18. Android之MVP模式实现登录和网络数据加载
  19. Sql 列转行 三种方法对比
  20. hdoj1114 Piggy-Bank

热门文章

  1. 2018年东北农业大学春季校赛 I wyh的物品【01分数规划/二分】
  2. Java爬虫系列之实战:爬取酷狗音乐网 TOP500 的歌曲(附源码)
  3. Network | 802.1x
  4. mac与linux服务器之间使用ssh互通有无
  5. readis 内部数据结构
  6. CentOS 挂载NTFS分区的两种方法
  7. json字符串调整
  8. 测试Apache服务器及httpd: Could not reliably determine the server&#39;s fully qualified domain name解决办法
  9. 【GLSL教程】(七)逐像素的光照 【转】
  10. 微信开发token验证失败