android下小文件上传
来源:互联网 发布:cnc程序模拟软件 编辑:程序博客网 时间:2024/06/05 08:05
客户端代码
package com.example.testuploadfile;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String uploadFile = "/sdcard/test2.jpg";
private String srcPath = "/sdcard/test2.jpg";
//private String actionUrl = "http://10.0.2.2:8888/MobilePoliceServer/TestUploadFileServlet";
private String actionUrl = "http://192.168.1.100:8888/MobilePoliceServer/TestUploadFileServlet";
private TextView mText1;
private TextView mText2;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText1 = (TextView) findViewById(R.id.myText1);
mText1.setText("文件路径:\n" + uploadFile);
mText2 = (TextView) findViewById(R.id.myText2);
mText2.setText("上传网址:\n" + actionUrl);
//设置Button的Onclick事件
mButton = (Button) findViewById(R.id.myButton);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
uploadFile();
}
});
}
//上传文件到Servlet的方法
private void uploadFile(){
String uploadUrl = "http://192.168.1.100:8888/MobilePoliceServer/TestUploadFileServlet";
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
//设置数据输出流目的地
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ srcPath.substring(srcPath.lastIndexOf("/") + 1)
+ "\"" + end);
dos.writeBytes(end);
//设置数据输入流来源
FileInputStream fis = new FileInputStream(srcPath);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();
} catch (Exception e){
e.printStackTrace();
setTitle(e.getMessage());
}
}
}
服务器端代码
package com.amaker.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class TestUploadFileServlet
*/
@WebServlet("/TestUploadFileServlet")
public class TestUploadFileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestUploadFileServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
String uploadPath = "C:\\upload\\";
File file = new File(uploadPath);
if(!file.exists()){
file.mkdir();
}
String filename = "";
InputStream is = null;
for(FileItem item : items){
if(item.isFormField()){
//处理普通表单域
if(item.getFieldName().equals("filename")){
if(!item.getString().equals("")){
filename = item.getString("UTF-8");
}
}
}
//处理上传文件
else if(item.getName() != null && !item.getName().equals("")){
filename = item.getName().substring(item.getName().lastIndexOf("\\") + 1);
is = item.getInputStream();//得到上传文件的InputStream对象
}
filename = uploadPath + filename;
if(new File(filename).exists()){
new File(filename).delete();
}
//开始上传文件
if(!filename.equals("")){
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[8192];
int count = 0;
while((count = is.read(buffer)) > 0){
//向服务器端文件写入字节流
fos.write(buffer, 0, count);
}
fos.close();
is.close();
out.println("文件上传成功!");
}
}
} catch (Exception e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
在手机上可以上传,在模拟器上好像没有反应。
- android下小文件上传
- android下的文件上传
- Android下大文件分割上传
- android下大文件分割上传
- android下大文件分割上传
- Android FTP 上传下传文件
- 文件上传小程序
- 小文件上传代码
- 文件上传小实例
- ajax 小文件上传
- 小程序文件上传
- Struts1 文件上传小例子
- MVC小练 上传文件
- 单文件上传小实例
- Struts2 文件上传小总结
- PHP上传文件小例子
- HttpUrlConnection实现小文件上传
- Uploadify文件上传小实例
- AlterDialog的使用方法
- Linux 使用积累
- Java中创建对象的四种方式
- C#下操作Visual Fox数据库
- Hibernate的基本映射数据类型
- android下小文件上传
- jqGrid基础学习:2第一个jqGrid
- Android开发 摄像头自动聚焦完美实现 --------------续上
- hdu 1878 欧拉回路
- ios程序内实现打电话
- spring缓存问题
- C#中的值类型与引用类型
- 人人都是产品经理 总结
- 正则表达式验证邮箱