package cn.demo.controller;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import cn.demo.domain.FileBean;
import cn.demo.service.IFileService;
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private IFileService fileService;
//使用springMVC解析器进行文件上传
@RequestMapping("/uploadToHDFS2.action")
public String uploadToHDFS2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException, Exception, URISyntaxException{
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为“”,说明该文件存在,否则说明该文件不存在
if(myFileName.trim() !=""){
System.out.println("======>"+myFileName);
//文件名
String fileName = file.getOriginalFilename();
String filePath = "/1127/"+fileName;
//--------------------------------------------
//上传到hdfs
Configuration configuration = new Configuration();
configuration.addResource("/core-site.xml");
configuration.addResource("/hdfs-site.xml");
FileSystem fs = FileSystem.get(new URI("hdfs://ns1"),configuration ,"root");
byte[] bytes = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(bytes);
FSDataOutputStream outputStream = fs.create(new Path(filePath));
IOUtils.copyBytes(inputStream, outputStream, 4096, true);
//记录上传文件
fileService.addFile(fileName,filePath);
//--------------------------------------------
}
}
}
}
return "success";
}
@RequestMapping("/uploadToHDFS.action")
public String uploadToHDFS(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response) throws Exception{
//拿到上传文件的输入流
FileInputStream inputStream = (FileInputStream) file.getInputStream();
String fileName = file.getOriginalFilename();
String filePath = "/1127/"+UUID.randomUUID()+fileName;
System.out.println("****************>"+fileName);
//上传到hdfs
Configuration configuration = new Configuration();
configuration.addResource("/core-site.xml");
configuration.addResource("/hdfs-site.xml");
FileSystem fs = FileSystem.get(new URI("hdfs://ns1"),configuration ,"root");
FSDataOutputStream outputStream = fs.create(new Path(filePath));
IOUtils.copyBytes(inputStream, outputStream, 4096, true);
return "success";
}
@RequestMapping("/listHDFS.action")
public String listHDFS(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
Configuration configuration = new Configuration();
configuration.addResource("/core-site.xml");
configuration.addResource("/hdfs-site.xml");
FileSystem fs = FileSystem.get(new URI("hdfs://ns1"),configuration ,"root");
ArrayList<FileBean> fileList = new ArrayList<FileBean>();
Path path = new Path("/1127");
FileStatus[] status;
try {
status = fs.listStatus(path);
for(FileStatus f : status){
fileList.add(new FileBean(f));
}
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileList",fileList);
return "success";
}
@RequestMapping("/download.action")
public String download(String path,String fileName,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
Configuration configuration = new Configuration();
configuration.addResource("/core-site.xml");
configuration.addResource("/hdfs-site.xml");
FileSystem fs = FileSystem.get(new URI("hdfs://ns1"),configuration ,"root");
String tempPath = new String(path.getBytes("ISO-8859-1"),"UTF-8");
String[] ps = tempPath.split("//ns1");
if(ps.length>1){
path = ps[1];
}else {
throw new Exception();
}
System.out.println(path);
FSDataInputStream inputStream = fs.open(new Path(path));
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename="+fileName);
//输出流
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = inputStream.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
inputStream.close();
bos.close();
return "success";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JavaWeb操作hadoop2.6 HDFS,从页面上传,下载,列表展示的demo (135个子文件)
FileController.class 8KB
FileBean.class 2KB
UserController.class 2KB
User.class 2KB
UserDaoImpl.class 1KB
UserServiceImpl.class 953B
ResFile.class 925B
FileServiceImpl.class 664B
IUserService.class 217B
IUserDao.class 205B
IFileService.class 181B
.classpath 886B
org.eclipse.wst.common.component 550B
org.eclipse.wst.jsdt.ui.superType.container 49B
hadoop-hdfs-2.4.1.jar 6.51MB
hadoop-common-2.4.1.jar 2.72MB
hadoop-hdfs-2.4.1-tests.jar 2.44MB
guava-11.0.2.jar 1.57MB
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 1.57MB
commons-math3-3.1.1.jar 1.53MB
hadoop-common-2.4.1-tests.jar 1.44MB
mockito-all-1.8.5.jar 1.35MB
netty-3.6.2.Final.jar 1.14MB
snappy-java-1.0.4.1.jar 973KB
jaxb-impl-2.2.3-1.jar 869KB
zookeeper-3.4.5.jar 762KB
jersey-server-1.9.jar 696KB
mysql-connector-java-5.1.5-bin.jar 661KB
jackson-mapper-asl-1.8.8.jar 653KB
org.springframework.context-3.0.0.RELEASE.jar 642KB
mybatis-3.0.3.jar 612KB
c3p0-0.9.1.jar 594KB
commons-collections-3.1.jar 546KB
org.springframework.beans-3.0.0.RELEASE.jar 539KB
jetty-6.1.26.jar 527KB
jets3t-0.9.0.jar 527KB
protobuf-java-2.5.0.jar 521KB
log4j-1.2.16.jar 470KB
jersey-core-1.9.jar 448KB
antlr-2.7.6.jar 433KB
httpclient-4.2.5.jar 423KB
jstl-1.2.jar 405KB
org.springframework.web.servlet-3.0.0.RELEASE.jar 378KB
org.springframework.jdbc-3.0.0.RELEASE.jar 373KB
org.springframework.core-3.0.0.RELEASE.jar 351KB
org.springframework.web-3.0.0.RELEASE.jar 351KB
org.springframework.orm-3.0.0.RELEASE.jar 322KB
org.springframework.aop-3.0.0.RELEASE.jar 317KB
dom4j-1.6.1.jar 307KB
commons-httpclient-3.1.jar 298KB
avro-1.7.4.jar 296KB
commons-configuration-1.6.jar 292KB
commons-lang-2.6.jar 278KB
commons-net-3.1.jar 267KB
commons-compress-1.4.1.jar 236KB
junit-4.8.2.jar 232KB
org.springframework.transaction-3.0.0.RELEASE.jar 226KB
httpcore-4.2.5.jar 222KB
jackson-core-asl-1.8.8.jar 222KB
commons-beanutils-core-1.8.0.jar 201KB
commons-beanutils-1.7.0.jar 184KB
jsch-0.1.42.jar 181KB
commons-io-2.4.jar 181KB
jetty-util-6.1.26.jar 173KB
org.springframework.expression-3.0.0.RELEASE.jar 151KB
jersey-json-1.9.jar 144KB
hadoop-nfs-2.4.1.jar 142KB
commons-digester-1.8.jar 140KB
commons-el-1.0.jar 110KB
com.springsource.org.apache.commons.io-1.4.0.jar 108KB
jaxb-api-2.2.2.jar 103KB
org.springframework.context.support-3.0.0.RELEASE.jar 99KB
jsp-api-2.1.jar 98KB
xz-1.0.jar 92KB
hadoop-hdfs-nfs-2.4.1.jar 86KB
jettison-1.1.jar 66KB
activation-1.1.jar 62KB
commons-logging-1.1.3.jar 61KB
com.springsource.org.apache.commons.logging-1.1.1.jar 60KB
commons-codec-1.4.jar 57KB
com.springsource.org.apache.commons.fileupload-1.2.0.jar 53KB
org.springframework.asm-3.0.0.RELEASE.jar 52KB
hadoop-auth-2.4.1.jar 49KB
asm-3.2.jar 42KB
commons-cli-1.2.jar 40KB
jsr305-1.3.9.jar 32KB
jackson-xc-1.8.8.jar 32KB
mybatis-spring-1.0.0-RC3.jar 31KB
org.springframework.aspects-3.0.0.RELEASE.jar 30KB
paranamer-2.3.jar 29KB
slf4j-api-1.6.1.jar 25KB
stax-api-1.0-2.jar 23KB
java-xmlbuilder-0.4.jar 18KB
jackson-jaxrs-1.8.8.jar 17KB
hadoop-annotations-2.4.1.jar 17KB
xmlenc-0.52.jar 15KB
slf4j-log4j12-1.7.2.jar 9KB
com.springsource.javax.annotation-1.0.0.jar 6KB
com.springsource.org.aopalliance-1.0.0.jar 5KB
FileController.java 6KB
共 135 条
- 1
- 2
FB_fighter
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页