package org.mbhcare.server;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.datastore.Blob;
import javax.jdo.Query;
import java.io.InputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileUpload extends HttpServlet {
private static final Logger log =
Logger.getLogger(FileUpload.class.getName());
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
//String diagnosis = req.getParameter("diagnosis");
PersistenceManager pm = PMF.get().getPersistenceManager();
// Query query = pm.newQuery(DiagnosisImage.class);
// query.setFilter("diagnosis == :diagnosis");
// List<DiagnosisImage> existences = (List<DiagnosisImage>)query.execute(diagnosis);
// for (DiagnosisImage item : existences){
// //pm.deletePersistent(item);
// }
//
ServletFileUpload upload = new ServletFileUpload();
//res.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
//log.warning("diagnosisIdParam: " + diagnosisIdParam);
if (item.isFormField()) {
//log.warning("Got a form field: " + item.getFieldName());
} else {
// log.warning("Got an uploaded file: " + item.getFieldName() +
// ", name = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
// int len;
// byte[] buffer = new byte[8192];
// while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
// res.getOutputStream().write(buffer, 0, len);
// }
Blob uploadedImage = new Blob(IOUtils.toByteArray(stream));
DiagnosisImage img = new DiagnosisImage("",uploadedImage);
//PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(img);
} finally {
pm.close();
}
res.setContentType("text/plain");
res.getOutputStream().write(img.getId().toString().getBytes());
//res.sendRedirect("/upload?uploadedImageKey=" + img.getId().toString());
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String uploadedImageKey = req.getParameter("uploadedImageKey");
resp.setHeader("Content-Type", "text/html");
// This is a bit hacky, but it'll work. We'll use this key in an Async service to
// fetch the image and image information
resp.getWriter().println(uploadedImageKey);
}
}