摘要:本文主要向大家介绍了MFC编程实例:从零实现MVC框架之文件上传(8),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
本文主要向大家介绍了MFC编程实例:从零实现MVC框架之文件上传(8),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
这里使用commons-io-2.4.jar和commons-fileupload-1.3.1.jar实现文件上传,所以事先要把这两个包加入到项目中。
文件解析类
[java] view plain copy
1. package com.hc.web.io;
2.
3. import java.util.Iterator;
4. import java.util.List;
5.
6. import javax.servlet.http.HttpServletRequest;
7.
8. import org.apache.commons.fileupload.FileItem;
9. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
10. import org.apache.commons.fileupload.servlet.ServletFileUpload;
11. import org.apache.commons.fileupload.servlet.ServletRequestContext;
12.
13. public class UploadFileParse {
14.
15. public static MutipartFile parseRequest(HttpServletRequest request) throws Exception {
16. MutipartFile mf = new MutipartFile();
17.
18. DiskFileItemFactory factory = new DiskFileItemFactory();
19. ServletFileUpload sfu = new ServletFileUpload(factory);
20. List<FileItem> items = sfu.parseRequest(new ServletRequestContext(request));
21.
22. Iterator<FileItem> iter = items == null ? null : items.iterator();
23. while (iter != null && iter.hasNext()) {
24. FileItem item = (FileItem) iter.next();
25. if (item.isFormField()) {
26. String field = item.getFieldName().trim();
27. String value = item.getString("UTF-8").trim();
28. mf.put(field, value);
29. } else if (!item.isFormField()) {
30. mf.setFileName(item.getName());
31. mf.setInputStream(item.getInputStream());
32. }
33. }
34. return mf;
35. }
36. }
文件信息类
[java] view plain copy
1. package com.hc.web.io;
2.
3. import java.io.File;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6. import java.io.InputStream;
7. import java.util.HashMap;
8. import java.util.Map;
9.
10. public class MutipartFile {
11.
12. private Map<String,String> params = new HashMap<>();
13. private InputStream inputStream;
14. private String fileName;
15.
16. public void put(String key,String value){
17. params.put(key, value);
18. }
19.
20. public void setInputStream(InputStream inputStream) {
21. this.inputStream = inputStream;
22. }
23.
24. public Map<String,String> getParams(){
25. return params;
26. }
27.
28. public String getString(String key){
29. return params.get(key)==null?null:params.get(key);
30. }
31.
32.
33. public String getFileName() {
34. return fileName;
35. }
36.
37. public void setFileName(String fileName) {
38. this.fileName = fileName;
39. }
40.
41. public void copyToFile(File file) throws IOException{
42. FileOutputStream fos = new FileOutputStream(file);
43. byte [] buffer = new byte[1024];
44. int len = -1;
45. while((len = inputStream.read(buffer)) != -1){
46. fos.write(buffer, 0, len);
47. }
48. fos.flush();
49. fos.close();
50. }
51.
52.
53.
54.
55. }
修改AbstractHcAction类
[java] view plain copy
1. //调用controller方法
2. Method method = controllerClassInfo.getMethodMap().get(methodParam);
3. Object invoke = null;
4. if(ServletFileUpload.isMultipartContent(request)){
5. MutipartFile mf = UploadFileParse.parseRequest(request);
6. invoke = method.invoke(newInstance, request,response,mf);
7. }else{
8. invoke = method.invoke(newInstance, request,response);
9. }
10. if(invoke != null && invoke instanceof View){
11. View view = (View)invoke;
12. request.getRequestDispatcher(view.getPath()).forward(request, response);
13. }
修改UserController类
[java] view plain copy
1. @MethodMapping(method="upload")
2. public View uploadFile(HttpServletRequest request, HttpServletResponse response,MutipartFile file) throws Exception{
3. String parameter = file.getString("file_name");
4. File targetFile = new File("c:/aaa.txt");
5. file.copyToFile(targetFile);
6.
7. return new View("user.jsp");
8. }
测试
需要一个jsp页面,页面上使用form表单上传一个txt文件,这里这个jsp页面的代码就不列出来了。form的action路径是
/上下文/user.hc?method=upload。
启动tomcat,浏览器输入上传jsp的地址,然后选择一个txt的文件,点击提交按钮。然后查看c盘下是不是多了一个aaa.txt
的文件。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号