Android 将文件上传到服务器
欢迎来到我的博客:
本文通过tomcat下servlet的方法使用服务器配置
服务器方面
public static void uploadManage(File uploadFile){
String boundary = "*****";
try {
URL url = new URL("http://xxxxxxx/upload/upload");//你的服务器地址
HttpURLConnection con = (HttpURLConnection)url.openConnection();
// 允许Input、Output,不使用Cache
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(50000);
con.setReadTimeout(50000);
// 设置传送的method=POST
con.setRequestMethod("POST");
//在一次TCP连接中可以持续发送多份数据而不会断开连接
con.setRequestProperty("Connection", "Keep-Alive");
//设置编码
con.setRequestProperty("Charset", "UTF-8");
//text/plain能上传纯文本文件的编码格式
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
con.setRequestProperty("filename",uploadFile.getName());
// 设置DataOutputStream
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
// 取得文件的FileInputStream
FileInputStream fStream = new FileInputStream(uploadFile);
// 设置每次写入1024bytes
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
// 从文件读取数据至缓冲区
while ((length = fStream.read(buffer)) != -1) {
// 将资料写入DataOutputStream中
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());
}
}
安卓
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("Hello Servlet!
");
response.getWriter().println(new Date().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
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";
}
}
一些补充
如果要获取文件名本地上传文件到服务器,可以在请求头中添加filename元素。
Android需要申请相关权限才能进行网络传输。申请方法请参考官方文档。
服务器搭建好后本地上传文件到服务器,就可以直接访问网上的上传地址了。如果看到 helloservlet,说明服务器已经搭建好了。
别忘了更新 web.xml 和 index.jsp 中的数据,没有更多的技巧。
杂项
我学习Android开发很久了,所以我不会从头开始更新我的笔记。我计划更新之前已经完成的一些更有用的 Android 技术的实现。
Android 会不时更新。
欢迎在各种Android、网络技术或AI神经网络上交流,网络前端和后端都可以交流。