Last active
November 5, 2021 13:15
-
-
Save chongma/c75c9092a0b7c5fdb36b44b1311945ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Path("/files/{id}/upload") | |
| @POST | |
| @Consumes(MediaType.MULTIPART_FORM_DATA) | |
| public Response createFileUpload(@PathParam("id") int id, @QueryParam("description") String description, | |
| Attachment attachment, @Context UriInfo uriInfo) { | |
| FileUpload item = uploadController.createFileUpload(id, description, attachment); | |
| URI uri = uriInfo.getAbsolutePathBuilder().path(Integer.toString(item.getId())).build(); | |
| return Response.created(uri).entity(item.getId()).build(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let formData = new FormData(); | |
| formData.append("file", this.fileData); | |
| axios.post('/files/12345/upload', formData, { | |
| params: { | |
| description: this.description, | |
| }, | |
| headers: { | |
| "Content-Type": "multipart/form-data", | |
| }, | |
| }) | |
| .then(() => {}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Transactional | |
| public UploadFile createFileUpload(int id, String description, Attachment attachment) { | |
| File file = fileDb.selectFile(fileId); | |
| if (!fileController.selectFileValid(file)) { | |
| throw new RestException(401); | |
| } | |
| UploadDescription uploadDescription = createUploadDescription(description); | |
| Upload upload = new Upload(uploadDescription, false); | |
| createUpload(upload, attachment); | |
| ... | |
| return uploadFile; | |
| } | |
| private void createUpload(Upload upload, Attachment attachment) { | |
| DataHandler handler = attachment.getDataHandler(); | |
| try { | |
| InputStream stream = handler.getInputStream(); | |
| MultivaluedMap<String, String> map = attachment.getHeaders(); | |
| String type = utilityDao.getFileExtension(utilityDao.getFileName(map)); | |
| upload.setType(type); | |
| uploadDb.create(upload); | |
| uploadDb.flush(); | |
| // check record was created | |
| if (upload.getId() != 0) { | |
| // create new path | |
| String path = selectUploadPath(upload); | |
| // move upload rename using upload id | |
| OutputStream out = new FileOutputStream(new java.io.File(path)); | |
| int read = 0; | |
| byte[] bytes = new byte[1024]; | |
| while ((read = stream.read(bytes)) != -1) { | |
| out.write(bytes, 0, read); | |
| } | |
| stream.close(); | |
| out.flush(); | |
| out.close(); | |
| } | |
| } catch (FileNotFoundException e) { | |
| emailController.emailStackTrace(e); | |
| } catch (IOException e) { | |
| emailController.emailStackTrace(e); | |
| } catch (Exception e) { | |
| if (e.getMessage().startsWith("scanning: ")) { | |
| throw new RestException(400, e.getMessage()); | |
| } else { | |
| emailController.emailStackTrace(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment