dcs vor 5 Tagen
Ursprung
Commit
9a5718b2b1

+ 41 - 4
virgo.file/src/main/java/com/bosshand/virgo/file/controller/FileNodeController.java

@@ -16,10 +16,7 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
 import java.net.URLEncoder;
 import java.util.Base64;
 import java.util.HashMap;
@@ -198,5 +195,45 @@ public class FileNodeController {
 		return Response.ok();
 		
 	}
+
+	/**
+	 * 打包下载生成的html页面
+	 */
+	@RequestMapping(value = "/filenode/zip/{id}", method = RequestMethod.GET)
+	public void getFileZip(@PathVariable int id, final HttpServletResponse response) {
+
+		logger.info("Ready to get file byte on parentId:" + id);
+		byte[] data = null;
+		OutputStream outputStream = null;
+		try {
+			FileNode fileNode = fileManagerService.get(id);
+			data = fileManagerService.getFileZip(id);
+			response.setCharacterEncoding("UTF-8");
+			response.setContentType("application/x-msdownload");
+			response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileNode.getName()+".zip", "UTF-8"));
+			OutputStream outputSream = response.getOutputStream();
+			outputSream.write(data);
+			outputSream.flush();
+		} catch (IOException e) {
+			throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e);
+		} finally {
+			if (outputStream != null) {
+				try {
+					outputStream.close();
+				} catch (IOException e) {
+				}
+			}
+		}
+	}
+
+	/**
+	 * 访问html页面
+	 */
+	@ApiOperation("html页面")
+	@RequestMapping(value = "/enterprise/{id}/{name}", method = RequestMethod.GET)
+	public String getHtml(@PathVariable int id, @PathVariable String name) throws UnsupportedEncodingException {
+		return fileManagerService.getHtml(id, name);
+	}
+
 	
 }

+ 29 - 3
virgo.file/src/main/java/com/bosshand/virgo/file/service/FileManagerService.java

@@ -9,10 +9,10 @@ import com.bosshand.virgo.file.util.ImageChange;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.util.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 @Service
 public class FileManagerService {
@@ -209,4 +209,30 @@ public class FileManagerService {
 		return fileNodeDao.getIds(list);
 	}
 
+	public byte[] getFileZip(int id) throws IOException {
+		List<FileNode> list = list(id);
+		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+		try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
+			for (FileNode node : list) {
+				byte[] aByte = getByte(node.getId());
+				ZipEntry zipEntry = new ZipEntry(node.getName());
+				zipOutputStream.putNextEntry(zipEntry);
+				zipOutputStream.write(aByte);
+				zipOutputStream.closeEntry();
+			}
+		}
+		return byteArrayOutputStream.toByteArray();
+	}
+
+	public String getHtml(int id, String name) throws UnsupportedEncodingException {
+		List<FileNode> list = list(id);
+		for (FileNode node : list) {
+			if (node.getName().equals(name)) {
+				byte[] aByte = getByte(node.getId());
+				return new String(aByte, "UTF-8");
+			}
+		}
+		return "";
+	}
+
 }