我是靠谱客的博主 年轻马里奥,这篇文章主要介绍springmvc服务端+android客户端的文件上传,现在分享给大家,希望可以做个参考。

一、Springmvc服务端

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

 

 

2. <!-- 配置上传的组件   用于SpringMvc查找commons包 -->

         <beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

</beans>

 

 

3.服务端

 

1)构建回传类

复制代码
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
Result.java ackage org.linfeng.action; import org.springframework.stereotype.Component; @Component public 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)构建控制器

复制代码
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
@Controller public 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

复制代码
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
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);//<input type="file" name="file"> //把请求实体设置到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线程调用工具类方法,注意,耗时操作必须另外开启一个工作线程执行

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start();




最后

以上就是年轻马里奥最近收集整理的关于springmvc服务端+android客户端的文件上传的全部内容,更多相关springmvc服务端+android客户端内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部