I have an Android app, and I want to send a file to my jsp server. I can conect with my jsp server, I can send the file, but I dont know how I can receive it. This is my code. I send image to the server, but it not received... I have a new file but it have 0 bits :SS
Sorry for my bad English
SERVER JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import ="java.util.List"%>
<%@ page import ="java.util.ArrayList"%>
<%@ page import ="java.io.BufferedReader"%>
<%@ page import ="java.io.InputStreamReader"%>
<%@ page import ="java.io.OutputStream"%>
<%@ page import ="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Recibir FIchero</title>
</head>
<body>
<%
// or "data"
System.out.println("Recibiendo Archivo...");
final String path = "C:\\Users\\Public";
final Part filePart = request.getPart("image");
final String fileName = "recibido.jpg";
OutputStream out2 = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();
try {
out2 = new FileOutputStream(new File(path + File.separator
+ fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out2.write(bytes, 0, read);
}
writer.println("New file " + fileName + " created at " + path);
} catch (FileNotFoundException fne) {
writer.println("You either did not specify a file to upload or are "
+ "trying to upload a file to a protected or nonexistent "
+ "location.");
writer.println("<br/> ERROR: " + fne.getMessage());
System.out.println("error!!!!");
fne.printStackTrace();
} finally {
if (out2 != null) {
out2.close();
System.out.println("canal cerrado!!!!");
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
System.out.println("fichero cerrado!!!!");
}
}
%>
</body>
</html>
ANDROID CLIENT
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient mHttpClient = new DefaultHttpClient(params);
String url = "http://81.36.204.206:8080/Servidor1/setFile.jsp";
File file = new File(archivo[0]);
try {
HttpPost httppost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Title", new StringBody("Title"));
multipartEntity.addPart("Nick", new StringBody("Nick"));
multipartEntity.addPart("Email", new StringBody("Email"));
multipartEntity.addPart("image", new FileBody(file));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
} catch (Exception e) {
e.printStackTrace();
}
return "";
archivo[0] example: "/sdcard/WhatsApp/Profile Pictures/34654826312.jpg"
If you want to recieve the file you have to write it to the external storage and you must request the WRITE_EXTERNAL_STORAGE
permission in your manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When saving a file to internal storage, you can acquire the appropriate directory as a File
by calling getFilesDir() or getCacheDir():
File file = new File(context.getFilesDir(), filename);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.