|
|
@@ -1,21 +1,25 @@
|
|
|
package com.uas.platform.b2b.controller;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.uas.dfs.service.FileClient;
|
|
|
+import com.uas.platform.b2b.dao.AttachDao;
|
|
|
+import com.uas.platform.b2b.dao.CommonDao;
|
|
|
+import com.uas.platform.b2b.model.Attach;
|
|
|
+import com.uas.platform.b2b.service.AttachService;
|
|
|
+import com.uas.platform.b2b.support.FileUploadHttp;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.ui.ModelMap;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
-import com.uas.platform.b2b.model.Attach;
|
|
|
-import com.uas.platform.b2b.service.AttachService;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 平台文件
|
|
|
@@ -33,6 +37,12 @@ public class FileController {
|
|
|
@Autowired
|
|
|
private FileClient fileClient;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private CommonDao commonDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AttachDao attachDao;
|
|
|
+
|
|
|
/**
|
|
|
* 文件下载
|
|
|
*
|
|
|
@@ -62,16 +72,114 @@ public class FileController {
|
|
|
// 其他的当做是存放在本地服务器上,去本地服务器根据文件路径去获取
|
|
|
File file = new File(attach.getPath());
|
|
|
if(!file.exists()) throw new IllegalArgumentException("附件不存在");
|
|
|
- response.addHeader("Content-Length", String.valueOf(file.length()));
|
|
|
- InputStream in = new FileInputStream(file);
|
|
|
- OutputStream os = response.getOutputStream();
|
|
|
- int data = 0;
|
|
|
- while ((data = in.read()) != -1) {
|
|
|
- os.write(data);
|
|
|
- }
|
|
|
- in.close();
|
|
|
- os.close();
|
|
|
+ InputStream in = new FileInputStream(path);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + new String(attach.getName().getBytes("utf-8"), "iso-8859-1"));
|
|
|
+ response.addHeader("Content-Length", String.valueOf(file.length()));
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("application/octec-stream");
|
|
|
+ int data = 0;
|
|
|
+ while ((data = in.read()) != -1) {
|
|
|
+ os.write(data);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ os.close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新文件路径到文件服务器
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/refreshPath", method = RequestMethod.GET)
|
|
|
+ public ModelMap refreshpath() throws IOException {
|
|
|
+ ModelMap map = new ModelMap();
|
|
|
+ List<Long> successIds = new ArrayList<Long>();
|
|
|
+ List<Long> failedIds = new ArrayList<Long>();
|
|
|
+ convertPath(successIds, failedIds);
|
|
|
+ map.put("successIds", successIds);
|
|
|
+ map.put("failedIds", failedIds);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归更新所有附件信息
|
|
|
+ *
|
|
|
+ * @param successIds
|
|
|
+ * @param failedIds
|
|
|
+ */
|
|
|
+ public void convertPath(List<Long> successIds, List<Long> failedIds) {
|
|
|
+ List<Attach> attaches = new ArrayList<Attach>();
|
|
|
+ String sql = "select at_id as \"id\",at_path path,at_size as \"size\",at_name as \"name\" from attachs where at_path like '/usr/local/wildfly8/%' and nvl(at_status, ' ')<>'failure' and rownum <= 50";
|
|
|
+ attaches = commonDao.query(sql, Attach.class);
|
|
|
+ if(!CollectionUtils.isEmpty(attaches)) {
|
|
|
+ for(Attach attach : attaches) {
|
|
|
+ String path = null;
|
|
|
+ String filePath = "E:\\file\\" + attach.getPath();//下载到本地再进行更新处理
|
|
|
+ File file = new File(filePath);
|
|
|
+ if(file.exists()) {
|
|
|
+ String name = attach.getName().substring(attach.getName().indexOf(".") + 1, attach.getName().length());
|
|
|
+ try {
|
|
|
+ InputStream in = new FileInputStream(filePath);
|
|
|
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while( (len=in.read(buffer)) != -1 ){
|
|
|
+ outStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ path = fileClient.upload(outStream.toByteArray(), attach.getSize(), name, null);
|
|
|
+// FileUploadHttp.Response response = FileUploadHttp.upload(null, filePath, null);
|
|
|
+// if (response.getStatusCode() == 200) {
|
|
|
+// JSONObject obj = JSONObject.parseObject(response.getResponseText());
|
|
|
+// if(obj.get("path").toString() != null) path = obj.get("path").toString();
|
|
|
+// }
|
|
|
+ } catch (IOException e) {
|
|
|
+ failedIds.add(attach.getId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(path != null) {
|
|
|
+ // update
|
|
|
+ sql = "update attachs set at_path = '" + path +"',at_status = 'success' where at_id = " + attach.getId();
|
|
|
+ commonDao.getJdbcTemplate().update(sql);
|
|
|
+ successIds.add(attach.getId());
|
|
|
+ } else {
|
|
|
+ // 上传失败更新状态为failure
|
|
|
+ sql = "update attachs set at_status = 'failure' where at_id = " + attach.getId();
|
|
|
+ commonDao.getJdbcTemplate().update(sql);
|
|
|
+ failedIds.add(attach.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ convertPath(successIds, failedIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @RequestMapping(value = "/test", method = RequestMethod.GET)
|
|
|
+ public ModelMap getPath() throws IOException {
|
|
|
+ return new ModelMap("path", getAttachCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("resource")
|
|
|
+ private String getAttachCode() throws FileNotFoundException, IOException {
|
|
|
+ String path = "E:\\file\\test\\11.jpg";// 文件路径
|
|
|
+ InputStream in = new FileInputStream(path);
|
|
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while( (len=in.read(buffer)) != -1 ){
|
|
|
+ output.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ byte[] imgByte = output.toByteArray();
|
|
|
+ path = fileClient.upload(imgByte, 373760, "jpg", null);
|
|
|
+// return URLDecoder.decode(Base64.toBase64String(imgByte), "UTF-8");
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
}
|