Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package entities.service;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.ws.rs.Consumes;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
- import org.glassfish.jersey.media.multipart.FormDataParam;
- /**
- *
- * @author HUYTRINH
- */
- @Path("/photo")
- public class FileUpload {
- private static final String SERVER_UPLOAD_LOCATION_FOLDER = "D://uploaded/";
- /**
- * Upload a File
- */
- @POST
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public Response uploadFile(
- @FormDataParam("file") InputStream fileInputStream,
- @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
- String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
- // save the file to the server
- saveFile(fileInputStream, filePath);
- String output = "File saved to server location : " + filePath;
- return Response.status(200).entity(output).build();
- }
- // save uploaded file to a defined location on the server
- private void saveFile(InputStream uploadedInputStream,
- String serverLocation) {
- try {
- OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
- int read = 0;
- byte[] bytes = new byte[1024];
- outpuStream = new FileOutputStream(new File(serverLocation));
- while ((read = uploadedInputStream.read(bytes)) != -1) {
- outpuStream.write(bytes, 0, read);
- }
- outpuStream.flush();
- outpuStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement