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-spms-java/src/main/java/com/glxp/api/upload/DownloadController.java

91 lines
2.8 KiB
Java

2 years ago
package com.glxp.api.upload;
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 {
@GetMapping(value = "/udiwms/image/register/file/getImage")
public void getImage(HttpServletResponse response
, @RequestParam String type
, @RequestParam String name) throws IOException {
if (name.endsWith("pdf")) {
OutputStream os = null;
try {
FileInputStream input = new FileInputStream(new File("d:/1s/udiwms/register/file/" + type + "/" + 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();
}
2 years ago
}
} else {
OutputStream os = null;
try {
// 读取图片
BufferedImage image = ImageIO.read(
new FileInputStream(new File("d:/1s/udiwms/register/file/" + 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();
}
2 years ago
}
}
2 years ago
}
@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(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();
}
}
}
}