安卓端上传多文件与servlet服务器接收多文件

时间:2022-01-26 04:00:57

android端上传文件很简单  用xutils提供的方法 如下:

 RequestParams requestParams=new RequestParams();
requestParams.addBodyParameter("video", new File(path));//视频 path为路径
requestParams.addBodyParameter("img", new File(path));//图片 path为路径
requestParams.addBodyParameter("user", "user");
HttpUtils httpUtils=new HttpUtils();
//http://localhost:8080/mytom/UploadServlet 是我写的服务器路径
httpUtils.send(HttpRequest.HttpMethod.POST,"http://localhost:8080/mytom/UploadServlet", requestParams, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
progressDialog.dismiss();
Toast.makeText(context,responseInfo.result,Toast.LENGTH_SHORT).show();
}


@Override
public void onFailure(HttpException e, String s) {
progressDialog.dismiss();
Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
}


@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
// Log.i("loadssss", "loadssss:" + (total));


float f=(float)current/total*100;
int len=(int)f;
Log.i("loadssss", "curssss:" + len);
progressDialog.setProgress(len);
//progressDialog 系统的进度dialog 去写上传的进度

}
});

servlet服务器用jspsmartupload_zh.jar去接收多文件,需要注意一点:如果xuitls不是上传文件只上传string类型的时候,servlet不能会调用jspsmartupload_zh.jar里提供的方法的,你只需按平常的方法去取传过来的string就行了。

servlet代码用jspsmartupload_zh去接收多文件(是解决中文乱码的jspsmartupload_zh)如下:

 

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(req, resp);
req.setCharacterEncoding("utf-8");

resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html,charset=UTF-8");
//req.getParameter("user")==null 是判断xutils是不是上传的文件 如果是 就用jspsmartUpload 去取文件
if(req.getParameter("user")==null){
SmartUpload smartUpload = new SmartUpload();


try {
smartUpload.initialize(this.getServletConfig(), req, resp);

smartUpload.upload();
//获取相对路径的文件夹 用来存放手机传过来的文件
String str=req.getSession().getServletContext().getRealPath("/data/");
File filedir=new File(str);
if(!filedir.exists()){
filedir.mkdirs();
}

String imgUrl="http://vidio.yika.net.cn:8080/mytom";

//传文件的时候 接收传文件传过来的参数string
String personID=smartUpload.getRequest().getParameter("user");
//获取所有传过来的文件
Files files=smartUpload.getFiles();

if(files!=null){

for(int i=0;i<files.getCount();i++){
com.jspsmart.upload.File file=files.getFile(i);
if(!file.isMissing()){

SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
String houzui=file.getFileName().substring(file.getFileName().lastIndexOf(".")+1);
//存放在指定的路径下
file.saveAs(filedir + "/"+sdf.format(new Date())+i+"."+houzui, smartUpload.SAVE_PHYSICAL);
//准备存放在数据库的文件路径
pics[i]=imgUrl + "/data/"+sdf.format(new Date())+i+"."+houzui;
}
}
//连接数据库上传数据
connection(personID,dates,resp);
}else{

}

}else{
//xutils没有上传文件的时候 用平常的方法去拿获取的string

String personID=req.getParameter("user");

connection(personID,dates,resp);
}

}

private void connection(String personID,String dates,HttpServletResponse resp) {
try {
System.out.println("开始连接");
Class.forName(driver);
System.out.println("连接中...");
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("连接成功");


String sql="写入插入的文件地址的信息";

PreparedStatement statement1=conn.prepareStatement(sql);
if(!statement1.execute()){
System.out.println("execute");
try {
resp.getWriter().println("添加成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
statement1.close();

resultSet.close();
conn.close();

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}



 最后附上xutils.jar与jspsmartupload_zh.jar地址

http://download.csdn.net/detail/u012303938/9385562