一、Springmvc服务端

1.引入文件上传的JAR包commons-fileupload-1.2.jar,commons-io-1.3.2.jar.

 

 

2.

        

 

 

3.服务端

 

1)构建回传类

Result.javaackage org.linfeng.action;import org.springframework.stereotype.Component;@Componentpublic class Result {private String path;private String code;private String message;public String getPath() {return path;}public void setPath(String path) {this.path = path;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public void setMessage(String message) {this.message = message;}public String getMessage() {return message;}}

2)构建控制器

@Controllerpublic class UploadController {         @Resource         privateResult result;          @RequestMapping("/mobile/uploadfile")         @ResponseBody      //把回传类转换成json         public   ResultuploadPhone(@RequestParam(value="file",required=false)MultipartFilefile,HttpServletRequest request) throws IllegalStateException, IOException{                   Stringpath=uploadFile(file, request);                   result.setCode("200");                   result.setPath(path);                   result.setMessage("上传成功");                   returnresult;         }         /**          * 上传          * @param file          * @param request          * @return          * @throws IOException          */         privateString  uploadFile(MultipartFile file,HttpServletRequest request)                            throwsIOException {                   Stringpath=request.getSession().getServletContext().getRealPath("upload");                   StringfileName=file.getOriginalFilename();                   FiletargetFile=new File(path,fileName);                   if(!targetFile.exists()){                            targetFile.mkdirs();                   }                   file.transferTo(targetFile);//               request.setAttribute("filePath",targetFile.getAbsolutePath());                   return    targetFile.getAbsolutePath();         }}

二、android客户端

 

 

1.导入上传需要的JAR包httpmime-4.1.1.jar

 

2.写一个工具类使用HttpClient

public class AndroidUploadFile {         /**          *          * @param filePath 文件地址          * @param urlServer  服务器的地址          * @return          * @throws IOException          * @throws ClientProtocolException          */         publicstatic  String uoloadFile(StringfilePath,String urlServer) throws ClientProtocolException, IOException{                   //使用HttpClient                   HttpClienthttpClient=new DefaultHttpClient();                   //必须设置请求的协议                   httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);                   //设置http post请求                   HttpPost  httpPost=new HttpPost(urlServer);                   //构建上传的文件的实体                   MultipartEntity  multipartEntity=new MultipartEntity();                   //构建文件的File的对象                   Filefile=new File(filePath);                   //添加文件的                   ContentBodycontentBody=new FileBody(file);                   multipartEntity.addPart("file",contentBody);//                   //把请求实体设置到HttpPost                   httpPost.setEntity(multipartEntity);                   //执行这个请求                   HttpResponseresponse=httpClient.execute(httpPost);                   //通过响应取到状态行                   StatusLinestatusLine= response.getStatusLine();                   //通过状态码去判断                   if(statusLine.getStatusCode()==HttpStatus.SC_OK){                            HttpEntity  entity=response.getEntity();                            Stringresult=EntityUtils.toString(entity);                            Log.i("TAG","*******"+result);                   }else{                            Log.i("TAG","请求出了问题");                   }                   returnnull;         }}

3.在UI线程调用工具类方法,注意,耗时操作必须另外开启一个工作线程执行

public  void doClick(View v){String path=Environment.getExternalStorageDirectory().getAbsolutePath();final File file=new File(path,"test.txt");new Thread(){public void run() {try {AndroidUploadFile.uoloadFile(file.getAbsolutePath(), "http://10.8.30.81:8080/SpringMvcUpload/mobile/uploadfile");} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}};}.start();




更多相关文章

  1. Android 将图片文件,转成Bitmap
  2. android 中生成XML文件的代码
  3. Android中pull解析XML文件
  4. Android获得文件夹大小
  5. android头文件路径
  6. Android上传文件到Django服务器
  7. Android: Intent.ACTION_SEND分享文件
  8. Android的drawable文件夹的说明

随机推荐

  1. 补0818:数据库ddl,dml实操 及 新建用户表
  2. PHP文件上传函数封装
  3. 补0817:域名空间内类实现自动加载,use的作
  4. 一分钟学会使用ApiPost中的全局参数和目
  5. php获取地址中的省市区
  6. 正经人一辈子都用不到的 JavaScript 方法
  7. 小鸟云云服务器可以绑定多个域名搭建多个
  8. 补0820作业: 图片上传原生PHP整理
  9. MVC架构模式,依赖注入,对象容器与门面技
  10. 小鸟云产品/服务初体验