我试图将图像文件上传到服务器。首先,我将图像路径本地存储在数据库中,然后使用该路径创建一个新文件,并尝试使用http post请求上传图像。它仅适用于2到3个图像文件,如果我尝试连续上传超过3个图像文件,则不会上传文件。我在网络方面有一个非常基本的想法.Plz请给我一个解决方案。
答案 0 :(得分:0)
你必须像这样工作
public void connectForMultipart() throws Exception {
con = (HttpURLConnection) ( new URL(url)).openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.connect();
os = con.getOutputStream();
}
public void addFormPart(String paramName, String value) throws Exception {
writeParamData(paramName, value);
}
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
os.write( (delimiter + boundary + "\r\n").getBytes());
os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes());
os.write( ("Content-Type: application/octet-stream\r\n" ).getBytes());
os.write( ("Content-Transfer-Encoding: binary\r\n" ).getBytes());
os.write("\r\n".getBytes());
os.write(data);
os.write("\r\n".getBytes());
}
public void finishMultipart() throws Exception {
os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
while ((sResponse = reader.readLine()) != null)
{
s = s.append(sResponse);
}
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
return s.toString();
}else
{
return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代替图像,您可以发送任何内容(在您的情况下为image
文件),您希望...如果它创建了一些heck然后
你应该将大文件分成小部分,然后尝试发送。并在服务器上加入这个小部分。
在我的情况下httpmime-4.2.1-1.jar
仅在此代码中需要
答案 1 :(得分:0)
参考此
Image Upload and Retrieve Android with WebServices (PHP Code)
Repository包含2个Android项目..图像从Mysql上传到Mysql和Image Retreival 一个文件夹包含webservices和mysql数据库
for Image Upload ...你只需要insert.php
如果它不起作用,请告诉我
答案 2 :(得分:0)
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//follwing line compress Bitmap image
image.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] byteArray = stream.toByteArray();
String s= Base64.encodeToString(byteArray, Base64.DEFAULT);
这里你将获得一个base64编码的图像,只需将String发送到服务器。你可以从服务器解码图像。
答案 3 :(得分:0)
使用它可以帮助你。
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
//FileBody bin = null;
MultipartEntity reqEntity = new MultipartEntity();
Bitmap rotatedBitmap2;
if(rotatedBitmap2 != null){
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
rotatedBitmap2.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data,"dcc/"+n+".jpg");
reqEntity.addPart("Image", bab);
}
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);
if (resEntity != null) {
//do some work
}
} catch (Exception ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
}