提交 b00fa7b6 编写于 作者: sq3536's avatar sq3536

file rest

上级 507fc9a3
......@@ -13,7 +13,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Hashtable;
@Slf4j
@RestController
......@@ -24,24 +24,76 @@ public class DiskCoreResource
@Autowired
private DiskCoreService diskCoreService;
private Hashtable<String, String> type = null;
private String getType(String ext)
{
if(type==null)
{
type = new Hashtable<String, String>();
type.put(".pdf", "application/pdf");
type.put(".jpg", "image/jpg");
type.put(".jpeg", "image/jpeg");
type.put(".bmp", "image/bmp");
type.put(".gif", "image/gif");
type.put(".tif", "image/tiff");
type.put(".tiff", "image/tiff");
type.put(".png", "image/png");
}
if(type.containsKey(ext.toLowerCase()))
return type.get(ext.toLowerCase());
else
return "application/octet-stream";
}
@PostMapping(value = "{folder}/upload")
public ResponseEntity<FileItem> upload(@PathVariable("folder") String folder,@RequestParam("file") MultipartFile multipartFile){
return ResponseEntity.ok().body(diskCoreService.saveFile(folder,multipartFile));
}
@PostMapping(value = "{folder}/upload/{id}")
public ResponseEntity<FileItem> updatefile(@PathVariable("folder") String folder,@PathVariable("id") String id,@RequestParam("file") MultipartFile multipartFile){
return ResponseEntity.ok().body(diskCoreService.saveFile(folder,id,multipartFile));
}
@GetMapping(value = "{folder}/download/{id}")
@ResponseStatus(HttpStatus.OK)
public void download(@PathVariable("folder") String folder, @PathVariable("id") String id, HttpServletResponse response){
File file= diskCoreService.getFile(folder,id);
File file = diskCoreService.getFile(folder,id);
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
this.sendRespose(response, file);
}
@GetMapping(value = "net-disk/download/{folder}/{id}/{name}.{ext}")
@ResponseStatus(HttpStatus.OK)
public void download(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable("name") String name, @PathVariable("ext") String ext, HttpServletResponse response){
File file = diskCoreService.getFile(folder,id);
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
this.sendRespose(response, file);
}
@GetMapping(value = "net-disk/{folder}/{id}/{name}.{ext}")
@GetMapping(value = "net-disk/file/{folder}/{id}/{name}.{ext}")
@ResponseStatus(HttpStatus.OK)
public void open(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable("name") String name, @PathVariable("ext") String ext, HttpServletResponse response){
File file= diskCoreService.getFile(folder,id);
File file = diskCoreService.getFile(folder,id);
String type = getType(ext);
response.setContentType(type);
if(type.toLowerCase().equals("application/octet-stream"))
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
this.sendRespose(response, file);
}
@GetMapping(value = "net-disk/preview/{folder}/{id}/{name}.{ext}")
@ResponseStatus(HttpStatus.OK)
public void preview(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable("name") String name, @PathVariable("ext") String ext, HttpServletResponse response){
File file = diskCoreService.getFile(folder,id);
String type = getType(ext);
response.setContentType(type);
if(type.toLowerCase().equals("application/octet-stream"))
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
this.sendRespose(response, file);
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册