|
@@ -7,7 +7,10 @@ import java.io.FileInputStream;
|
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -22,6 +25,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
|
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
|
|
+import com.xintong.system.err.BusinessException;
|
|
|
|
|
+
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping(value = "file")
|
|
@RequestMapping(value = "file")
|
|
|
public class FileController extends BaseController {
|
|
public class FileController extends BaseController {
|
|
@@ -40,22 +46,25 @@ public class FileController extends BaseController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "upload")
|
|
@RequestMapping(value = "upload")
|
|
|
- public void UploadFile(HttpServletRequest req, MultipartHttpServletRequest multiReq) {
|
|
|
|
|
|
|
+ public String UploadFile(HttpServletRequest req, MultipartHttpServletRequest multiReq) {
|
|
|
initfile();
|
|
initfile();
|
|
|
// 获取上传文件的路径
|
|
// 获取上传文件的路径
|
|
|
- String uploadFilePath = multiReq.getFile("file1").getOriginalFilename();
|
|
|
|
|
|
|
+ String uploadFilePath = multiReq.getFile("file").getOriginalFilename();
|
|
|
System.out.println("uploadFlePath:" + uploadFilePath);
|
|
System.out.println("uploadFlePath:" + uploadFilePath);
|
|
|
// 截取上传文件的文件名
|
|
// 截取上传文件的文件名
|
|
|
String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1,
|
|
String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\\') + 1,
|
|
|
uploadFilePath.indexOf('.'));
|
|
uploadFilePath.indexOf('.'));
|
|
|
System.out.println("multiReq.getFile()" + uploadFileName);
|
|
System.out.println("multiReq.getFile()" + uploadFileName);
|
|
|
|
|
+
|
|
|
|
|
+ uploadFileName = UUID.randomUUID().toString();
|
|
|
|
|
+
|
|
|
// 截取上传文件的后缀
|
|
// 截取上传文件的后缀
|
|
|
String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
|
|
String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
|
|
|
System.out.println("uploadFileSuffix:" + uploadFileSuffix);
|
|
System.out.println("uploadFileSuffix:" + uploadFileSuffix);
|
|
|
FileOutputStream fos = null;
|
|
FileOutputStream fos = null;
|
|
|
FileInputStream fis = null;
|
|
FileInputStream fis = null;
|
|
|
try {
|
|
try {
|
|
|
- fis = (FileInputStream) multiReq.getFile("file1").getInputStream();
|
|
|
|
|
|
|
+ fis = (FileInputStream) multiReq.getFile("file").getInputStream();
|
|
|
fos = new FileOutputStream(new File(path + uploadFileName + ".") + uploadFileSuffix);
|
|
fos = new FileOutputStream(new File(path + uploadFileName + ".") + uploadFileSuffix);
|
|
|
byte[] temp = new byte[1024];
|
|
byte[] temp = new byte[1024];
|
|
|
int i = fis.read(temp);
|
|
int i = fis.read(temp);
|
|
@@ -72,6 +81,7 @@ public class FileController extends BaseController {
|
|
|
fis.close();
|
|
fis.close();
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
|
+ throw new BusinessException(20001);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (fos != null) {
|
|
if (fos != null) {
|
|
@@ -79,9 +89,14 @@ public class FileController extends BaseController {
|
|
|
fos.close();
|
|
fos.close();
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
|
+ throw new BusinessException(20001);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ Map<String, String> data=new HashMap<>();
|
|
|
|
|
+ data.put("path", req.getContextPath()+"file/download/"+uploadFileName + "." + uploadFileSuffix);
|
|
|
|
|
+
|
|
|
|
|
+ return returnResult(0, "获取成功", data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
|
|
@RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
|
|
@@ -127,7 +142,7 @@ public class FileController extends BaseController {
|
|
|
System.out.println("文件接受成功了");
|
|
System.out.println("文件接受成功了");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @RequestMapping(value = "/download/{filename:[a-zA-Z0-9.]+}", method = RequestMethod.GET)
|
|
|
|
|
|
|
+ @RequestMapping(value = "/download/{filename:[a-zA-Z0-9-.]+}", method = RequestMethod.GET)
|
|
|
public void Download(HttpServletResponse res, @PathVariable String filename) {
|
|
public void Download(HttpServletResponse res, @PathVariable String filename) {
|
|
|
String fileName = filename;
|
|
String fileName = filename;
|
|
|
res.setHeader("content-type", "application/octet-stream");
|
|
res.setHeader("content-type", "application/octet-stream");
|