测试网页:
控制台调用:
@RestController
@EnableAutoConfiguration
public class HelloController {
//网页访问路径
@RequestMapping(value = "/download", method = {RequestMethod.GET})
public ModelAndView download() {
ModelAndView view = new ModelAndView();
view.setViewName("download");
return view;
}
//下载单文件
@RequestMapping(value = "/down", method = {RequestMethod.POST, RequestMethod.GET})
public ResponseEntitydown(HttpServletRequest request) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + "mp3");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
HDFSClient hdfsClient = new HDFSClient();
InputStream in = hdfsClient.down1("/DataOwner1/Folder_txt/file.mp3");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(in.available())
.contentType(MediaType.parseMediaType("application/octet-stream"))
// .body(new ZipOutputStream(zipOutputStream));
.body(new InputStreamResource(in));
}
//下载文件夹
@RequestMapping(value = "/downDir", method = {RequestMethod.POST, RequestMethod.GET})
public ResponseEntitydownDir(HttpServletRequest request) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".zip");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
String cloudPath = "/DataOwner2/Folder1";
HDFSClient hdfsClient = new HDFSClient();
ByteArrayOutputStream zos = (ByteArrayOutputStream) hdfsClient.down2(cloudPath);
byte[] out = zos.toByteArray();
zos.close();
ResponseEntityresponse = new ResponseEntity<>(out, headers, HttpStatus.OK);
return response;
}
}
具体实现:
@Service
public class HDFSClient{
//单文件
public InputStream down1(String cloudPath) throws IOException, InterruptedException, URISyntaxException {
// 1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");
FSDataInputStream in = fs.open(new Path(cloudPath));
return in;
}
//多文件
public OutputStream down2(String cloudPath) throws IOException, InterruptedException, URISyntaxException {
// 1获取对象
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(out);
compress(cloudPath, zos, fs);
zos.close();
return out;
}
public void compress(String baseDir, ZipOutputStream zipOutputStream, FileSystem fs) throws IOException {
FileStatus[] fileStatulist = fs.listStatus(new Path(baseDir));
//
System.out.println("basedir = " + baseDir);
String[] strs = baseDir.split("/");
String lastName = strs[strs.length - 1];
//zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (int i = 0; i < fileStatulist.length; i++) {
String name = fileStatulist[i].getPath().toString().substring(25);
name = name.substring(name.indexOf("/"+lastName));
if (fileStatulist[i].isFile()) {
Path path = fileStatulist[i].getPath();
FSDataInputStream inputStream = fs.open(path);
System.out.println("fileStatulist[i].getPath().getName()" + fileStatulist[i].getPath().getName());
zipOutputStream.putNextEntry(new ZipEntry( name));
IOUtils.copyBytes(inputStream, zipOutputStream, 1024);
inputStream.close();// Folder2 1.m3p
} else {
System.out.println(fileStatulist[i].getPath().getName() + "/");
zipOutputStream.putNextEntry(new ZipEntry(name + "/"));
System.out.println("fileStatulist[i].getPath().toString().substring(25) = " + fileStatulist[i].getPath().toString().substring(25));
compress(fileStatulist[i].getPath().toString().substring(25), zipOutputStream, fs);
}
}
}
}