|
@@ -0,0 +1,135 @@
|
|
|
+package com.zskk.dicom.monitor.uploader;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.DataInputStream;
|
|
|
+import java.io.DataOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import com.zskk.dicom.monitor.config.Configs;
|
|
|
+import com.zskk.dicom.monitor.report.ErrReporter;
|
|
|
+import com.zskk.dicom.monitor.utils.ExceptionUtil;
|
|
|
+import com.zskk.dicom.monitor.utils.MonitorFileUtils;
|
|
|
+
|
|
|
+public class FileUploader {
|
|
|
+
|
|
|
+ public static void upload(File file) {
|
|
|
+ File dir = file.getParentFile();
|
|
|
+ try {
|
|
|
+ String boundaryKey = UUID.randomUUID().toString().replaceAll("-", "").substring(8, 24);
|
|
|
+ String content = "\r\n----" + boundaryKey + "\r\n" + "Content-Type: application/octet-stream\r\n" + "Content-Disposition: form-data; name=\"" + renameFileName(file.getName()) + "\"; filename=\"" + renameFileName(file.getName()) + "\"\r\n" + "Content-Transfer-Encoding: binary\r\n\r\n";
|
|
|
+ String postUrl = "http://" + Configs.postHost + ":" + Configs.postPort + Configs.postUri;
|
|
|
+ Boolean uploadResult = uploadToUrl(postUrl, boundaryKey, content, "", file);
|
|
|
+ // 创建目录
|
|
|
+ String targetFile = MonitorFileUtils.touchBackDir(file);
|
|
|
+ if (uploadResult == true) {
|
|
|
+ // 上传完成后,移除文件
|
|
|
+ boolean isSucDel = file.renameTo(new File(targetFile));
|
|
|
+ if (!isSucDel) {
|
|
|
+ // 文件没有成功删除
|
|
|
+ ErrReporter.report("文件移动失败:" + file.getCanonicalPath() + " target:" + targetFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 移除空的目录
|
|
|
+ MonitorFileUtils.removeEmptyDir(dir);
|
|
|
+ } catch (Exception e) {
|
|
|
+ ErrReporter.report(ExceptionUtil.getExceptionTxt(e));
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String renameFileName(String name) {
|
|
|
+ return Configs.hospitalId + "-" + name;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("static-access")
|
|
|
+ private static boolean uploadToUrl(String url, String boundaryKey, String contentBinary, String fileId, File fromFile) throws Exception {
|
|
|
+ String uri = url;
|
|
|
+ String enddata = "\r\n----" + boundaryKey + "--";
|
|
|
+ // 获取文件输入流
|
|
|
+ InputStream in = null;
|
|
|
+ try {
|
|
|
+ in = new FileInputStream(fromFile);
|
|
|
+ } catch (FileNotFoundException fnfe) {
|
|
|
+ // 有些图片格式,会被系统临时占用,抛出文件被占用异常
|
|
|
+ Thread.currentThread().sleep(1000);
|
|
|
+ in = new FileInputStream(fromFile);
|
|
|
+ }
|
|
|
+ URL urlObj = new URL(uri);
|
|
|
+ HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
|
|
|
+ try {
|
|
|
+ con.setRequestMethod("POST"); // 设置关键值,以Post方式提交表单,默认get方式
|
|
|
+ con.setDoInput(true);
|
|
|
+ con.setDoOutput(true);
|
|
|
+ con.setUseCaches(false); // post方式不能使用缓存
|
|
|
+ // 设置请求头信息
|
|
|
+ // con.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ con.setRequestProperty("Charset", "UTF-8");
|
|
|
+ // 设置边界
|
|
|
+ con.setRequestProperty("Content-Type", "multipart/form-data; boundary=--" + boundaryKey);
|
|
|
+ // 设置文件大小
|
|
|
+ con.setRequestProperty("Content-Length", String.valueOf(fromFile.length()));
|
|
|
+ // 请求正文信息
|
|
|
+ // 第一部分:
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(contentBinary);
|
|
|
+ // 未知文件类型,以流的方式上传
|
|
|
+ byte[] head = sb.toString().getBytes("utf-8");
|
|
|
+ // 获得输出流
|
|
|
+ OutputStream out = new DataOutputStream(con.getOutputStream());
|
|
|
+ // 输出表头
|
|
|
+ out.write(head);
|
|
|
+ // 文件正文部分
|
|
|
+ // 把文件以流文件的方式 推入到url中
|
|
|
+ DataInputStream dataIn = new DataInputStream(in);
|
|
|
+ int bytes = 0;
|
|
|
+ byte[] bufferOut = new byte[1024];
|
|
|
+ while ((bytes = dataIn.read(bufferOut)) != -1) {
|
|
|
+ out.write(bufferOut, 0, bytes);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ // 结尾部分
|
|
|
+ byte[] foot = (enddata).getBytes("utf-8");// 定义最后数据分隔线
|
|
|
+ out.write(foot);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 读取返回数据
|
|
|
+ StringBuilder strBuf = new StringBuilder();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
|
+ try {
|
|
|
+ String line = null;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ strBuf.append(line).append("\n");
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ reader.close();
|
|
|
+ }
|
|
|
+ String rs = strBuf.toString();
|
|
|
+ Configs.sysLog.info("The file (" + fromFile.getName() + ") uploaded ! result:" + rs);
|
|
|
+ if (strBuf.indexOf("success\":true") > 0) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ con.disconnect();
|
|
|
+ con = null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|