Servlet 上传文件
2015-09-25 21:08
381 查看
package com.busymonkey; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.hadoop.fs.*; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.IOUtils; public class HadoopDownload extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); fileControl(req, resp); } /** * 上传文件的处理 */ private void fileControl(HttpServletRequest req, HttpServletResponse resp) throws ServletException { // 在解析请求之前先判断请求类型是否为文件上传类型 boolean isMultipart = ServletFileUpload.isMultipartContent(req); // 文件上传处理工厂 FileItemFactory factory = new DiskFileItemFactory(); // 创建文件上传处理器 ServletFileUpload upload = new ServletFileUpload(factory); // 开始解析请求信息 List items = null; try { items = upload.parseRequest(req); } catch (FileUploadException e) { e.printStackTrace(); } // 对所有请求信息进行判断 Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // 信息为普通的格式 if (item.isFormField()) { String fieldName = item.getFieldName(); String value = item.getString(); // String basePath = req.getRealPath("/home/hou/"); req.setAttribute(fieldName, value); } // 信息为文件格式 else { String fileName = item.getName(); int index = fileName.lastIndexOf("\\"); fileName = fileName.substring(index + 1); fileName = "123456.txt"; req.setAttribute("realFileName", fileName); // 将文件写入 // String path = req.getContextPath(); // String directory = "uploadFile"; // String basePath = "/home/hou/workspace/"; // String basePath = req.getRealPath("/home/hou/"); String basePath = "/home/hou/workspace/"; File file = new File(basePath, fileName); try { item.write(file); String target = "hdfs://10.19.155.41:9000/newfile/123.txt"; FileInputStream fis = new FileInputStream(new File("/home/hou/workspace/123456.txt")); Configuration config = new Configuration(); FileSystem fs = FileSystem.get(URI.create(target), config); OutputStream os = fs.create(new Path(target)); IOUtils.copyBytes(fis, os, 4096, true); } catch (Exception e) { e.printStackTrace(); } } } // try { // req.getRequestDispatcher("/download").forward(req, resp); // } // catch (IOException e) { // e.printStackTrace(); // } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <title>文件上传</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="download" method="post" enctype="multipart/form-data"> 文件别名:<input type="text" name="filename"><br> 选择文件:<input type="file" name="fileupload"><br> <input type="submit" value="提交"> </form> </body> </html>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hadoop</groupId>
<artifactId>hadoopDownload</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hadoopDownload Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
<!-- -->
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.7</version>
<scope>system</scope>
<systemPath>/home/hou/java/jdk1.7.0_79/lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<finalName>hadoopDownload</finalName>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <javaee:display-name>Archetype Created Web Application</javaee:display-name> <servlet> <servlet-name>HadoopDownload</servlet-name> <servlet-class>com.busymonkey.HadoopDownload</servlet-class> </servlet> <servlet-mapping> <servlet-name>HadoopDownload</servlet-name> <url-pattern>/download</url-pattern> </servlet-mapping> </web-app>
相关文章推荐
- HTTP协议 (三) 压缩
- js的数据处理记录
- 黑马程序员—Java概述
- C基础--栈的静态和动态生成
- libc、glibc和glib
- QT5--arm移植
- iOS跑马灯效果的源代码
- c++当某个函数很长时,却不想分写多个函数的解决办法
- 素数筛选法的进一步升级
- 如何申请Fusion 360 免费版
- 左右c++与java中国的垃圾问题的分析与解决
- UVa 12657 Boxes in a Line(应用双链表)
- Android中MediaButtonReceiver广播监听器的机制分析
- noip2010 接水问题 (模拟)
- hdu 2222(ac自动机)
- MongoDB性能优化
- 输入年月日输出该日期是当年的第几天的java代码
- Android平台一日游
- HTTP状态
- 今日学习总结细节错误