I try to send an image to using the multipart, like I read here https://stackoverflow.com/a/2937140/1552648 but when it arrive to the servlet it doesn't take any thing.
Here is the android java code, where I send some text and the image that I have the IMG_URL from de SD card
private void enviarAlServidorConFoto(Incidencia incide, String IMG_URL){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urlservidor + "AnyadirIncidenciaAndroid");
try {
//MultipartEntity multiPart = new MultipartEntity();
MultipartEntity multiPart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multiPart.addPart("usuario", new StringBody(incide.getUsuario()));
multiPart.addPart("latitud", new StringBody(incide.getLatitud()+""));
multiPart.addPart("longitud", new StringBody(incide.getLongitud()+""));
multiPart.addPart("descripcion", new StringBody(incide.getDescripcion()));
multiPart.addPart("tipo", new StringBody(incide.getTipo()));
// YA la pongo desde la web a normal postParametros.add(new BasicNameValuePair("prioridad", value));
multiPart.addPart("foto", new StringBody(incide.getFoto()));
//no se pasa el nombre de la foto, puesto que ya lo extraera cuando se mande la foto sola
multiPart.addPart("FotoIncidenciaAndroid", new FileBody(new File(IMG_URL)));
httpPost.setEntity(multiPart);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
HttpResponse res = httpClient.execute(httpPost);
res.getEntity().getContent().close();
} catch (ClientProtocolException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
Log.d("EnviadoAweb", e.toString());
}
}
Then, here is the servlet with multipart also.
@WebServlet(name="AnyadirIncidenciaAndroid", urlPatterns={"/android/AnyadirIncidenciaAndroid"}, loadOnStartup=1)
@MultipartConfig(location="c:\temp", fileSizeThreshold=1024*1024, maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5) public class AnyadirIncidenciaAndroid extends HttpServlet {
private static final long serialVersionUID = -6704540377265399620L;
/**
* @see HttpServlet#HttpServlet()
*/
public AnyadirIncidenciaAndroid() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final String usuario = request.getParameter("usuario");
System.out.println("Usuario recuperado al dar la incidencia desde movil: " + usuario);
IncidenciaDAO idao = new IncidenciaDAO();
Timestamp fechaalta = new Timestamp(System.currentTimeMillis());
Timestamp fechafin = null;
double latitud = Double.parseDouble(request.getParameter("latitud"));
double longitud = Double.parseDouble(request.getParameter("longitud"));
String descripcion = request.getParameter("descripcion");
String tipo = request.getParameter("tipo");
String estado = "Nueva";
String prioridad = "Normal";
String empleado = null; //al tratarse de una incidencias nueva, aun no tenemos ningun empleado que la halla resuelto
String foto = request.getParameter("foto");
String nombrefoto = null;
/*
* Si tenemos foto creada en la aplicacion movil, entonces tendremos que guardar el nuevo
* nombre de la incidencia, en caso, de no tener foto, pondremos una string vacio,
* y pondremos la imagen por defecto de sin imagen.
*/
Part p1 = request.getPart("FotoIncidenciaAndroid");
if(foto.equalsIgnoreCase("Si")){
try {
nombrefoto = idao.RenombrarFoto();
// guardamos la foto con su nombre
p1.write(nombrefoto);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
idao.anyadirIncidenciaMapaWeb(fechaalta, fechafin, latitud, longitud, descripcion, tipo, estado, prioridad, usuario, empleado, foto, nombrefoto);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
How I have to do to take from what I send from de request? Because that show me that the variable usuario is null
final String usuario = request.getParameter("usuario"); System.out.println("Usuario recuperado al dar la incidencia desde movil: " + usuario);
Also the log fomr the server say that it can make the request because what it send it's not a multipart
Thanks in advance.