我用base64上传图片,为什么用线程传不到服务器
2017-01-02 09:41
351 查看
String url = Definition.PATH_SERVLET + "ImageUpServlet";
int count = 0;
final String tempPath = count + "png";
ByteArrayOutputStream baso = new ByteArrayOutputStream();
bitmaps[0].compress(Bitmap.CompressFormat.JPEG, 100, baso);
byte aa[] = baso.toByteArray();
//Log.i("yy", String.valueOf(aa.length));
//DialogHelper.showAlertDialog(PostActivity.this, "图片大小为"+aa.length/1024 +"KB");
final String photo = Base64.encodeToString(aa,0, aa.length,Base64.DEFAULT);
//一张图片大概40KB
//Log.i("yy", String.valueOf(photo.length()));
Map<String, String> params = new HashMap<String, String>();
params.put("photo",photo);
params.put("name", tempPath);
StringBuilder sb = new StringBuilder(url);
sb.append('?');
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), "UTF-8"))
.append('&');
} //这是拼接地址 http://192.168.23.4:8080/Neighbours/servlet/GetConInfoServlet?.....
sb.deleteCharAt(sb.length() - 1);
URL urls = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode(); //此时code会等于400 服务器无响应
if (code == 200) {
System.out.println("sssssssssssss");
} else{
System.out.println("fffffffffffffffs");
}
服务端代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String photo = request.getParameter("photo");
String name = request.getParameter("name");
try {
// 对base64数据进行解码 生成 字节数组,不能直接用Base64.decode();进行解密
byte[] photoimg = new BASE64Decoder().decodeBuffer(photo);
for (int i = 0; i < photoimg.length; ++i) {
if (photoimg[i] < 0) {
// 调整异常数据
photoimg[i] += 256;
}
}
// byte[] photoimg = Base64.decode(photo);//此处不能用Base64.decode()方法解密,我调试时用此方法每次解密出的数据都比原数据大 所以用上面的函数进行解密,在网上直接拷贝的,花了好几个小时才找到这个错误(菜鸟不容易啊)
System.out.println("图片的大小:" + photoimg.length);
File file = new File("e:", "decode.jpg");
File filename = new File("e:\\name.txt");
if (!filename.exists()) {
file.createNewFile();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
FileOutputStream out1 = new FileOutputStream(filename);
out1.write(name.getBytes());
out.write(photoimg);
out.flush();
out.close();
out1.flush();
out1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count = 0;
final String tempPath = count + "png";
ByteArrayOutputStream baso = new ByteArrayOutputStream();
bitmaps[0].compress(Bitmap.CompressFormat.JPEG, 100, baso);
byte aa[] = baso.toByteArray();
//Log.i("yy", String.valueOf(aa.length));
//DialogHelper.showAlertDialog(PostActivity.this, "图片大小为"+aa.length/1024 +"KB");
final String photo = Base64.encodeToString(aa,0, aa.length,Base64.DEFAULT);
//一张图片大概40KB
//Log.i("yy", String.valueOf(photo.length()));
Map<String, String> params = new HashMap<String, String>();
params.put("photo",photo);
params.put("name", tempPath);
StringBuilder sb = new StringBuilder(url);
sb.append('?');
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), "UTF-8"))
.append('&');
} //这是拼接地址 http://192.168.23.4:8080/Neighbours/servlet/GetConInfoServlet?.....
sb.deleteCharAt(sb.length() - 1);
URL urls = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode(); //此时code会等于400 服务器无响应
if (code == 200) {
System.out.println("sssssssssssss");
} else{
System.out.println("fffffffffffffffs");
}
服务端代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String photo = request.getParameter("photo");
String name = request.getParameter("name");
try {
// 对base64数据进行解码 生成 字节数组,不能直接用Base64.decode();进行解密
byte[] photoimg = new BASE64Decoder().decodeBuffer(photo);
for (int i = 0; i < photoimg.length; ++i) {
if (photoimg[i] < 0) {
// 调整异常数据
photoimg[i] += 256;
}
}
// byte[] photoimg = Base64.decode(photo);//此处不能用Base64.decode()方法解密,我调试时用此方法每次解密出的数据都比原数据大 所以用上面的函数进行解密,在网上直接拷贝的,花了好几个小时才找到这个错误(菜鸟不容易啊)
System.out.println("图片的大小:" + photoimg.length);
File file = new File("e:", "decode.jpg");
File filename = new File("e:\\name.txt");
if (!filename.exists()) {
file.createNewFile();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
FileOutputStream out1 = new FileOutputStream(filename);
out1.write(name.getBytes());
out.write(photoimg);
out.flush();
out.close();
out1.flush();
out1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
相关文章推荐
- android通过Base64往服务器上传图片和对象
- Android 通过Base64上传图片到服务器
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- Android 通过Base64上传图片到服务器
- 根据base64转换成图片并上传服务器
- ios客户端base64上传图片到java服务器遇到的问题
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- App端通过Base64多张图片上传到服务器,并存入数据库数据
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- 上传图片,为什么在本地wamp上的图片上传正常,移动到服务器上就出现问题了?
- android通过Base64往服务器上传图片和对象
- Base64Coder方式实现android向jsp服务器后台上传图片
- 图片压缩,Base64编码后上传服务器
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- Android 通过Base64上传图片到服务器
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- 上传图片到图片服务器,base64转图片