You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/upload/DownloadController.java

118 lines
3.7 KiB
Java

package com.glxp.api.upload;
import com.glxp.api.constant.FileConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
@RestController
public class DownloadController {
@Value("${file_path}")
private String filePath;
@GetMapping(value = "/udiwms/image/register/file/getImage")
public void getImage(HttpServletResponse response
, @RequestParam String type
, @RequestParam String name) throws IOException {
if (name.endsWith("pdf") || name.endsWith("doc")) {
OutputStream os = null;
try {
FileInputStream input = new FileInputStream(new File(filePath + FileConstant.COMMON_FILE_PATH + name));
OutputStream out = response.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
input.close();
} catch (IOException e) {
} finally {
if (os != null) {
os.flush();
os.close();
}
}
} else {
OutputStream os = null;
try {
// 读取图片
BufferedImage image = ImageIO.read(
new FileInputStream(new File(filePath + "/register/" + type + "/" + name)));
response.setContentType("image/png");
os = response.getOutputStream();
if (image != null) {
ImageIO.write(image, "png", os);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
os.flush();
os.close();
}
}
}
}
@GetMapping(value = "/udiwms/donwload/file")
public void download(HttpServletResponse response,
@RequestParam String fileName) throws IOException {
OutputStream os = null;
try {
FileInputStream input = new FileInputStream(new File(filePath + "/pdf/template/"+fileName));
OutputStream out = response.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
input.close();
} catch (IOException e) {
} finally {
if (os != null) {
os.flush();
os.close();
}
}
}
@GetMapping(value = "/udiwms/donwload/printFile")
public void printFile(HttpServletResponse response, @RequestParam String fileName) throws IOException {
String url = filePath + "pdf/template/" + fileName;
OutputStream os = null;
try {
FileInputStream input = new FileInputStream(new File(url));
OutputStream out = response.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
input.close();
} catch (IOException e) {
} finally {
if (os != null) {
os.flush();
os.close();
}
}
}
}