Ho un'applicazione client in Android che utilizza HttpURLConnection
per inviare file al server. Il server utilizza l'API FileUpload di Apache Commons per analizzare i valori del modulo.ServletFileUpload # parseRequest (richiesta) restituisce una lista vuota
Il HttpURLConnection
invia questa richiesta:
-----------------------------4912995119421
Content-Disposition: form-data; name="deviceid"
9428103
-----------------------------4912995119421
Content-Disposition: form-data; name="countryid"
598
-----------------------------4912995119421
Content-Disposition: form-data; name="number"
98621360
-----------------------------4912995119421
Content-Disposition: form-data; name="file"; filename="2012-12-08 17.42.18.jpg"
Content-Type: image/jpeg
ÿØÿá1 Exif II*
@ ° ª
² ¼ Ä ( 1 Ì 2 Ø i‡ ì %ˆ \ n SAMSUNG GT-S5360L H H S5360LUHLB1 2012:12:08 17:42:18 š‚ î ?‚ ö "ˆ 'ˆ È ? 0220? þ ? ‘ ’ & ’
’ . 0100 @ ° > £ ¤ ¤ ¤ 6 ¤
2012:12:08 17:42:18 2012:12:08 17:42:18
d R98 0100 ( ¤ T. ÿØÿà JFIF ÿÛ C @@ÿÛ
-----------------------------4912995119421--
Il codice del server:
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") == -1)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
long maxFileSize = (2 * 1024 * 1024);
int maxMemSize = (2 * 1024 * 1024);
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax(maxFileSize);
List fileItems = upload.parseRequest(request);
Iterator i = fileItems.iterator();
//leo primero todas las variables.
int deviceID = 0;
int countryID = 0;
String phoneNumber = "";
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (fi.isFormField()) {
String variable = fi.getFieldName();
if (variable.equals("deviceid")) {
deviceID = Integer.parseInt(fi.getString());
} else if (variable.equals("countryid")) {
countryID = Integer.parseInt(fi.getString());
} else if (variable.equals("number")) {
phoneNumber = String.valueOf(Long.parseLong(fi.getString()));
}
}
}
if (deviceID == 0 || countryID == 0 || phoneNumber.equals("")) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
Il problema è nella linea List fileItems = upload.parseRequest(request);
. L'elenco restituito è vuoto e non riesco a ottenere i valori dei dati del modulo.
ho controllato utilizzando: request.getInputStream() byte [] a = new byte [8096]; int r = 0; ServletInputStream s = request.getInputStream(); Stringa c = ""; while ((r = s.read (a))! = -1) { c + = new String (a) .substring (0, r); } E ricevuto i dati ok. Il problema è che il caricamento del file di comando non può analizzare il contenuto. – toto
Bene, se si rimuove la chiamata 'request.getInputStream()', allora dovrebbe funzionare correttamente, esattamente come spiegato nella mia risposta. – BalusC
Ok, presumo che tu non stia chiamando in anticipo nessuno dei metodi menzionati. Questo problema può quindi significare solo che non hai impostato l'intestazione del limite corretto o quando hai utilizzato le nuove righe errate. Puoi trovare un esempio concreto e corretto nella parte inferiore di http://stackoverflow.com/a/2793153, nella sezione "Caricamento di file". – BalusC