Android上传文件到服务器
本文使用的是通过tomcat下servlet的方式进行的服务器配置
服务器方面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public static void uploadManage(File uploadFile){ String boundary = "*****"; try { URL url = new URL("http://xxxxxxx/upload/upload"); HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false);
con.setConnectTimeout(50000); con.setReadTimeout(50000); con.setRequestMethod("POST"); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); con.setRequestProperty("filename",uploadFile.getName()); DataOutputStream ds = new DataOutputStream(con.getOutputStream());
FileInputStream fStream = new FileInputStream(uploadFile); int bufferSize = 1024; byte[] buffer = new byte[bufferSize];
int length = -1; while ((length = fStream.read(buffer)) != -1) { ds.write(buffer, 0, length); } ds.flush(); fStream.close(); ds.close(); if(con.getResponseCode() == 200){ System.out.println("文件上传成功!上传文件为:" + uploadFile); } } catch (Exception e) { e.printStackTrace(); Log.e("Http","报错信息toString:" + e.toString()); System.out.println("文件上传失败!上传文件为:" + uploadFile); System.out.println("报错信息toString:" + e.toString()); } }
|
Android端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import java.io.*; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class UploadServlet extends HttpServlet { String path="/home/AndroidTest"; String filename; public void doGet(HttpServletRequest request, HttpServletResponse response){ try { response.getWriter().println("<h1>Hello Servlet!</h1>"); response.getWriter().println(new Date().toString()); } catch (IOException e) { e.printStackTrace(); } }
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException { InputStream is = request.getInputStream(); DataInputStream dis = new DataInputStream(is); filename=request.getHeader("filename"); System.out.println(filename+"+++++++++++"); String result = ""; try { result = saveFile(dis); } catch (Exception e) { System.out.println(e); result = "upload error!!!"; } System.out.println(result);
request.getSession().invalidate(); response.setContentType("image/jpeg;charset=UTF-8"); ObjectOutputStream dos = new ObjectOutputStream( response.getOutputStream()); dos.writeObject(result); dos.flush(); dos.close(); dis.close(); is.close(); } private String saveFile(DataInputStream dis) { File file = new File(path+"/"+filename); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { System.out.println(e); } } FileOutputStream fps = null; try { fps = new FileOutputStream(file); } catch (FileNotFoundException e) { System.out.println(e); } int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int length = -1;
try { while ((length = dis.read(buffer)) != -1) { fps.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } try { fps.flush(); fps.close(); } catch (IOException e) { e.printStackTrace(); System.out.println(e); } return "success"; } }
|
一些补充
如果想获得文件名的话可以在请求头head中加入filename元素。
Android要进行网络传输需要进行相关的权限申请,申请方法请见官方文档。
服务端搭好后可以直接在网络上访问上传地址,如果看到hello servlet就说明服务端搭建完成。
不要忘记在web.xml和index.jsp中进行更新数据,方法不再赘述。
后记
Android开发是很久以前学的,因此不打算从头开始更新笔记了,准备更新之前完成的一些比较有用的Android技术的实现方法。
Android会不定期更新。
欢迎进行各类Android,web技术,或AI神经网络的交流,web前后端都可以交流。