利用hdfs搭建网盘

利用hdfs搭建网盘–webserver开发,描述下实现思路:

1、网盘系统中的webserver是用来给用户提供操作界面,接收用户指令,完成文件上传、下载、图片上传、下载和图片预览功能的。
2、其中关于存储相关的功能都是调用hdfs API来完成,而关于文件的相关结构化信息都存储在mysql关系型数据库中;
3、webserver起到的是连接客户和hdfs的作用
4、采用的是SSH框架(Struts2、spring、hibernate)、数据库为mysql,数据模型请参考:利用hdfs搭建网盘–数据模型设计
5、web调用hdfs API的思路是:利用java运行时 运行java jar包,可参考《利用HDFS java API增删改查操作》,例如:

process = Runtime.getRuntime().exec("java -jar /root/hdfs-0.0.1-SNAPSHOT.jar read "+fileInfo.getFilePath()+" /root/file-tmp/"+fileInfo.getFileName());

文件列表页面:

@Action(value = "right", results = { @Result(name ="success", location ="/WEB-INF/npage/right.jsp") })
public String right() {
HttpSession session = this.getRequest().getSession();
User user = (User) session.getAttribute("user");
if (user != null) {
page = fileInfoServie.queryUserFileList(0, 20, user.getUserId());
}
return "success";
}

文件下载:

@Action(value = "downloadFile", results = { @Result(name ="success", location ="/right", type ="redirectAction") })
public String downloadFile(){
// 获取用户信息
HttpSession session = this.getRequest().getSession();
User user = (User) session.getAttribute("user");
//查出要删除的文件信息
String fileId = getRequest().getParameter("fileId");
List lists = fileInfoServie.findBy("fileId", Long.parseLong(fileId), PropertyFilter.MatchType.EQ);
FileInfo fileInfo = new FileInfo();
if(lists != null && lists.size() > 0){
fileInfo = lists.get(0);
}
//路径
String path = "/root/file-tmp/"+fileInfo.getFileName();
// 从hdfs取得文件
Process process;
try {
process = Runtime.getRuntime().exec("java -jar /root/hdfs-0.0.1-SNAPSHOT.jar read "+fileInfo.getFilePath()+" /root/file-tmp/"+fileInfo.getFileName());
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(newFileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
this.getResponse().reset();
// 设置response的Header
this.getResponse().addHeader("Content-Disposition","attachment;filename="+ new String(filename.getBytes()));
this.getResponse().addHeader("Content-Length",""+ file.length());
OutputStream toClient = new BufferedOutputStream(this.getResponse().getOutputStream());
this.getResponse().setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}

删除文件:

@Action(value = "deleteFile", results = { @Result(name ="success", location ="/right", type ="redirectAction") })
public String deleteFile(){
// 获取用户信息
HttpSession session = this.getRequest().getSession();
User user = (User) session.getAttribute("user");
//查出要删除的文件信息
String fileId = getRequest().getParameter("fileId");
List lists = fileInfoServie.findBy("fileId", Long.parseLong(fileId), PropertyFilter.MatchType.EQ);
FileInfo fileInfo = new FileInfo();
if(lists != null && lists.size() > 0){
fileInfo = lists.get(0);
}
// 将文件从hadoop集群删除
Process process;
try {
process = Runtime.getRuntime().exec("java -jar /root/hdfs-0.0.1-SNAPSHOT.jar delete "+fileInfo.getFilePath());
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
//从数据库删除
fileInfoServie.deleteById(fileInfo.getId());
return "success";
}

照片列表:

@Action(value = "goPicPage", results = { @Result(name ="success", location ="/WEB-INF/npage/pic-right.jsp") })
public String goPicPage() {
HttpSession session = this.getRequest().g
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值