import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
@Path("/helloworld")
public class HelloWorldResource
{
File filesDir = new File(System.getProperty("user.home"));
@Path("/image")
public Response getImage(@QueryParam("userid") String userId)
{
if (StringUtils.isBlank(userId)) {
return Response.status(Status.BAD_REQUEST).build();
}
File f = new File(filesDir + File.separator + userId + ".jpg");
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
byte b[] = new byte[(int) f.length()];
fis.read(b);
return Response.status(Status.OK).type("image/jpeg").entity(b).build();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Response.status(Status.BAD_REQUEST).build();
}
@Path("/image")
public Response uploadImage(@QueryParam("userid") String userId,
@Context HttpServletRequest request)
{
if (StringUtils.isBlank(userId)) {
return Response.status(Status.BAD_REQUEST).build();
}
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DiskFileItemFactory fileFactory = new DiskFileItemFactory();
fileFactory.setRepository(filesDir);
ServletFileUpload uploader = new ServletFileUpload(fileFactory);
if (!ServletFileUpload.isMultipartContent(request)) {
return Response.status(Status.BAD_REQUEST).build();
}
try {
List<FileItem> fileItemsList = uploader.parseRequest(request);
Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
while (fileItemsIterator.hasNext()) {
FileItem fileItem = fileItemsIterator.next();
System.err.println("FieldName=" + fileItem.getFieldName());
System.err.println("FileName=" + fileItem.getName());
System.err.println("ContentType=" + fileItem.getContentType());
System.err.println("Size in bytes=" + fileItem.getSize());
if (StringUtils.isNotBlank(fileItem.getName())) {
File file = new File(filesDir + File.separator + fileItem.getName());
System.err.println("Absolute Path at server=" + file.getAbsolutePath());
fileItem.write(file);
}
}
return Response.status(Status.OK).entity("upload success").build();
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return Response.status(Status.NOT_FOUND).build();
}
}