1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8.  
  9. import javax.servlet.ServletContext;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.annotation.MultipartConfig;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.Part;
  17.  
  18. /**
  19. * Servlet implementation class FileUploadServlet
  20. */
  21. @WebServlet(name = "FileUploadServlet", urlPatterns = {"/upload"})
  22. @MultipartConfig
  23. public class FileUploadServlet extends HttpServlet {
  24. private static final long serialVersionUID = 1L;
  25.  
  26. protected void processRequest(HttpServletRequest request,
  27. HttpServletResponse response)
  28. throws ServletException, IOException {
  29. response.setContentType("text/html;charset=utf-8");
  30. request.setCharacterEncoding("utf-8");
  31. ServletContext servletContext = this.getServletContext();
  32. String realPath = servletContext.getRealPath("/upload");
  33. final Part filePart = request.getPart("file");
  34. final String fileName = getFileName(filePart);
  35.  
  36. OutputStream out = null;
  37. InputStream filecontent = null;
  38. final PrintWriter writer = response.getWriter();
  39.  
  40. try {
  41. System.out.println(realPath + "@@@@@@@@@@@@@@@@@@@@@@@@@");
  42. System.out.println( File.separator + "@@@@@@@@@@@@@@@@@@@@@@@@@");
  43. System.out.println( fileName + "@@@@@@@@@@@@@@@@@@@@@@@@@");
  44. out = new FileOutputStream(
  45. new File(realPath + File.separator + fileName));
  46. filecontent = filePart.getInputStream();
  47.  
  48. int read;
  49. final byte[] bytes = new byte[1024];
  50.  
  51. while ((read=filecontent.read(bytes)) != -1) {
  52. out.write(bytes, 0, read);
  53. }
  54. writer.println("upload file " + fileName + " to path " + realPath);
  55. } catch (FileNotFoundException fne) {
  56. writer.println("no upload path or upload path is wrong.");
  57. writer.println("<br/> error: " + fne.getMessage());
  58. } finally {
  59. if (out !=null) {
  60. out.close();
  61. }
  62. if (filecontent != null){
  63. filecontent.close();
  64. }
  65. if (writer != null) {
  66. writer.close();
  67. }
  68. }
  69. }
  70.  
  71. private String getFileName(final Part part) {
  72. // TODO Auto-generated method stub
  73. for (String content: part.getHeader("content-disposition").split(";")) {
  74. System.out.println(content + "####################");
  75. if (content.trim().startsWith("filename")) {
  76. String filename = content.substring(
  77. content.lastIndexOf("\\") + 1).trim().replace("\"", "");
  78. System.out.println(filename + "####################");
  79. return "2.zip";
  80.  
  81. }
  82. }
  83. return null;
  84. }
  85.  
  86. /**
  87. * @see HttpServlet#HttpServlet()
  88. */
  89. public FileUploadServlet() {
  90. super();
  91. // TODO Auto-generated constructor stub
  92. }
  93.  
  94. /**
  95. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  96. */
  97. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  98. // TODO Auto-generated method stub
  99. processRequest(request, response);
  100. }
  101.  
  102. /**
  103. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  104. */
  105. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  106. // TODO Auto-generated method stub
  107. processRequest(request, response);
  108. }
  109.  
  110. }
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Login</title>
  6. </head>
  7. <body>
  8.  
  9. <form method="POST" action="upload" enctype="multipart/form-data">
  10. choose a file:
  11. <input type="file" name=file id="file" /><br/><br/>
  12. <input type="submit" value="upload" name="upload" id="upload" />
  13. </form>
  14.  
  15. </body>
  16. </html>

  

JAVA SERVLET上传文件的样码的更多相关文章

  1. java servlet上传文件并把文件内容显示在网页中

    servlet3.0(JDK1.6)自带的API即可实现本地文件的上传,Servlet3.0新增了Part接口,HttpServletRequest的getPart()方法取得Part实现对象.下面我 ...

  2. JAVA servlet 上传文件(commons-fileupload, commons-io)

    <1>获取二进制文件流并输出 InputStream inputStream = request.getInputStream(); BufferedReader reader = new ...

  3. Servlet上传文件

    Servlet上传文件 1.准备工作 (1)利用FileUpload组件上传文件,须要到apache上下载commons-fileupload-1.3.1.jar 下载地址:http://common ...

  4. 原生Servlet 上传文件

    依赖jar <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons ...

  5. java 后台上传文件

    java 后台上传文件 public static String uploadFile(File file, String RequestURL) throws IOException { Strin ...

  6. 使用Servlet上传文件

    使用浏览器向服务器上传文件其本质是打开了一个长连接并通过TCP方式传输数据.而需要的动作是客户端在表单中使用file域,并指定该file域的name值,然后在form中设定enctype的值为mult ...

  7. Java ftp上传文件方法效率对比

    Java ftp上传文件方法效率对比 一.功能简介: txt文件采用ftp方式从windows传输到Linux系统: 二.ftp实现方法 (1)方法一:采用二进制流传输,设置缓冲区,速度快,50M的t ...

  8. Java+Selenium 上传文件,点击选择“浏览文件”按钮,报错invalid argument

    Java+Selenium 上传文件,点击选择"浏览文件"按钮,报错invalid argument 解决代码: Actions action=new Actions(driver ...

  9. servlet上传文件报错(三)

    1.具体报错如下 null null Exception in thread "http-apr-8686-exec-5" java.lang.OutOfMemoryError: ...

随机推荐

  1. SetConsoleCtrlHandler

    Excerpt: Registering a Control Handler Function   This is an example of the SetConsoleCtrlHandler fu ...

  2. LC.exe已退出,代码为-1

    解决方法就是把Properties文件下的license.licx给删除,重新编译,这样就可以了.

  3. 《Cracking the Coding Interview》——第18章:难题——题目8

    2014-04-29 03:10 题目:给定一个长字符串S和一个词典T,进行多模式匹配,统计S中T单词出现的总个数. 解法:这是要考察面试者能不能写个AC自动机吗?对面试题来说太难了吧?我不会,所以只 ...

  4. 天性 & 如水一般,遇强则强 —— 梦想、行为、性格

    开篇声明,我博客中“小心情”这一系列,全都是日记啊随笔啊什么乱七八糟的.如果一不小心点进来了,不妨直接关掉.我自己曾经写过一段时间的日记,常常翻看,毫无疑问我的文笔是很差的,而且心情也是瞬息万变的.因 ...

  5. 【UVA10655】 Contemplation! Algebra

    题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...

  6. Swift 与众不同的地方

    Swift 与众不同的地方 switch(元组) 特点 其他语言中的switch语句只能比较离散的整形数据(字符可以转换成整数) 但是swift中可以比较整数.浮点数.字符.字符串.和元组数据类型,而 ...

  7. Pythonyield使用浅析

    转自:https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 您可能听说过,带有 yield 的函数在 Python ...

  8. 常用模块(random)

    import randomimport string# dt = random.randint(1,2) # 从1-2间取随机数,包括1.2# dt = random.randrange(1,3) # ...

  9. App自动化测试前期准备---android SDK配置

    说明:就是配置android SDK 一.sdk下载 Windows(X64):立即下载 Linux(X64):立即下载 二.Windows配置 1.解压文件 直接解压到指定目录(演示目录:D:/) ...

  10. python 学习分享-paramiko模块

    paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Solaris, BS ...