提交 24050f7c 作者: passer

文件服务器下载共享文件接口

父级 3fdce195
...@@ -3,7 +3,6 @@ package com.file.center.controller; ...@@ -3,7 +3,6 @@ package com.file.center.controller;
import com.file.center.util.CopyImageDTO; import com.file.center.util.CopyImageDTO;
import com.file.center.util.ImageUtils; import com.file.center.util.ImageUtils;
import com.file.center.util.OCRAPI;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -21,6 +20,7 @@ public class OCRToolController { ...@@ -21,6 +20,7 @@ public class OCRToolController {
@PostMapping("/copyImage") @PostMapping("/copyImage")
public String copyImage(@RequestBody CopyImageDTO dto) throws Exception { public String copyImage(@RequestBody CopyImageDTO dto) throws Exception {
return ImageUtils.downloadByURL(dto.getUrlString(), dto.getFileName(), OCRAPI.getImagePre(),dto.getLinux()); System.out.println("参数是:" + dto.toString());
return ImageUtils.downloadByURL(dto);
} }
} }
...@@ -11,7 +11,7 @@ public class CopyImageDTO { ...@@ -11,7 +11,7 @@ public class CopyImageDTO {
private String fileName; private String fileName;
private Boolean isLinux; private boolean isLinux;
private String savePath = OCRAPI.getImagePre(); private String savePath = OCRAPI.getImagePre();
...@@ -33,13 +33,6 @@ public class CopyImageDTO { ...@@ -33,13 +33,6 @@ public class CopyImageDTO {
this.fileName = fileName; this.fileName = fileName;
} }
public Boolean getLinux() {
return isLinux;
}
public void setLinux(Boolean linux) {
isLinux = linux;
}
public String getSavePath() { public String getSavePath() {
return savePath; return savePath;
...@@ -48,4 +41,22 @@ public class CopyImageDTO { ...@@ -48,4 +41,22 @@ public class CopyImageDTO {
public void setSavePath(String savePath) { public void setSavePath(String savePath) {
this.savePath = savePath; this.savePath = savePath;
} }
@Override
public String toString() {
return "CopyImageDTO{" +
"urlString='" + urlString + '\'' +
", fileName='" + fileName + '\'' +
", isLinux=" + isLinux +
", savePath='" + savePath + '\'' +
'}';
}
public boolean isLinux() {
return isLinux;
}
public void setLinux(boolean linux) {
isLinux = linux;
}
} }
package com.file.center.util; package com.file.center.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
...@@ -20,18 +17,14 @@ public class ImageUtils { ...@@ -20,18 +17,14 @@ public class ImageUtils {
/** /**
* 根据图片的URL从网页下载图片 * 根据图片的URL从网页下载图片
* *
* @param urlString 图片URL地址 * @param dto 图片URL地址
* @param filename 下载后的图片名称
* @param savePath 保存路径
* @throws Exception * @throws Exception
*/ */
public static String downloadByURL(String urlString, String filename, String savePath, Boolean isLinux) throws Exception { public static String downloadByURL(CopyImageDTO dto) throws Exception {
String FileSeparator = "\\";
if (isLinux) {
FileSeparator = "/";
}
// 构造URL // 构造URL
URL url = new URL(urlString); URL url = new URL(dto.getUrlString());
// 打开连接 // 打开连接
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
// 设置请求超时为5s // 设置请求超时为5s
...@@ -44,11 +37,11 @@ public class ImageUtils { ...@@ -44,11 +37,11 @@ public class ImageUtils {
// 读取到的数据长度 // 读取到的数据长度
int len; int len;
// 输出的文件流 // 输出的文件流
File sf = new File(savePath); File sf = new File(dto.getSavePath());
if (!sf.exists()) { if (!sf.exists()) {
sf.mkdirs(); sf.mkdirs();
} }
OutputStream os = new FileOutputStream(sf.getPath() + FileSeparator + "part" + FileSeparator + filename); OutputStream os = new FileOutputStream(sf.getPath()+File.separator+dto.getFileName() );
// 开始读取 // 开始读取
while ((len = is.read(bs)) != -1) { while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len); os.write(bs, 0, len);
...@@ -56,7 +49,7 @@ public class ImageUtils { ...@@ -56,7 +49,7 @@ public class ImageUtils {
// 完毕,关闭所有链接 // 完毕,关闭所有链接
os.close(); os.close();
is.close(); is.close();
return savePath + FileSeparator + "part" + FileSeparator + filename; return dto.getSavePath() ;
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论