下载:

1.在spring-mvc中配置(用于100M以下的文件下载)
  1. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
  2. <property name="messageConverters"> 
  3. <list> 
  4. <!--配置下载返回类型-->
  5. <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> 
  6.  
  7. <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
  8. <!--配置编码方式-->
  9. <property name="supportedMediaTypes" value="application/json; charset=UTF-8" /> 
  10. </bean> 
  11. </list> 
  12. </property> 
  13. </bean>
下载文件代码
  1. @RequestMapping("/file/{name.rp}")
    public ResponseEntity<byte[]> fileDownLoad(@PathVariable("name.rp")String name, HttpServletRequest request,HttpServletResponse response) {
    // @PathVariable String name,
    // @RequestParam("name")String name,
    // System.out.println("<name>"+name);
    // System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    ResponseEntity<byte[]> re = null;
    try {
    /**
    * css,js,json,gif,png,bmp,jpg,ico,doc,docx,xls,xlsx,txt,swf,pdf
    * **/
    //下载防止静态加载干扰
    Feelutile f=new Feelutile();
    name=f.getfileformat(name);

    String pathString="C:\\tempDirectory\\"+name;
    File file=new File(pathString);
    HttpHeaders headers=new HttpHeaders();
    //String filename=URLEncoder.encode(name, "UTF-8");//为了解决中文名称乱码问题
    String filename=new String(name.getBytes("utf-8"),"utf-8");
    byte[] by=FileUtils.readFileToByteArray(file);
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    //URLEncoder.encode(filename, "UTF-8")
    headers.setContentDispositionFormData("attachment",filename);
    headers.setContentLength(by.length);
    re=new ResponseEntity<byte[]>(by, headers, HttpStatus.CREATED);
    } catch (Exception e) {
    e.printStackTrace();
    try {
    request.getRequestDispatcher("/error/404.jsp").forward(request, response);
    } catch (ServletException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    return re;
    }

上传文件:
1在spring-mvc中配置
  1. <!--4.文件上传 配置 file upload -->
  2.     <bean id="multipartResolver"
  3.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  4.         <property name="defaultEncoding">
  5.             <value>UTF-8</value>
  6.         </property>
  7.         <property name="maxUploadSize">
  8.             <value>1048576000</value>
  9.         </property>
  10.         <property name="maxInMemorySize">
  11.             <value>40960</value>
  12.         </property>
  13.     </bean>
 

在controller中代码如下

  1. @RequestMapping(value="/upload", method = RequestMethod.POST)
  2.     @ResponseBody
  3.     public Json upload(Doc doc, @RequestParam("uploadFile") CommonsMultipartFile file) {
  4.         Json j = new Json();
  5.         
  6.         try {
  7.             String realpath = this.servletContext.getRealPath("/upload");            
  8.             String uploadFileFileName = file.getOriginalFilename();            
  9.             String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ", "");        
  10.             String fileType = uploadFileFileNameWithoutSpace.substring(uploadFileFileNameWithoutSpace.lastIndexOf("."));
  11.             
  12.             File targetFile = new File(realpath+File.separator, uploadFileFileNameWithoutSpace);
  13.             if (targetFile.exists()) {
  14.                 targetFile.delete();
  15.             }
  16.             file.getFileItem().write(targetFile);        
  17.             docService.upload(doc,uploadFileFileNameWithoutSpace);
  18.             
  19.             j.setSuccess(true);
  20.             j.setMsg("Upload manual successfully");
  21.             
  22.         }catch (Exception e) {
  23.             logger.error(ExceptionUtil.getExceptionMessage(e));
  24.             j.setMsg("Upload manual unsuccessfully");
  25.         }
  26.         
  27.         return j;
  28.     }  

java-spring-mvc_上傳下載文件配置及controller方法的更多相关文章

  1. spring 上传 下載文件

    1,spring配置文件添加文件上传配置 <!-- 上传文件 --> <bean id="multipartResolver" class="org.s ...

  2. linux环境下给文件加密/解密的方法

      原文地址:linix环境下给文件加密/解密的方法 作者:oracunix 一. 利用 vim/vi 加密:优点:加密后,如果不知道密码,就看不到明文,包括root用户也看不了:缺点:很明显让别人知 ...

  3. robots.txt文件配置和使用方法详解

    robots.txt文件,提起这个概念,可能不少站长还很陌生:什么是robots.txt文件?robots.txt文件有什么作用?如何配置robots.txt文件?如何正确使用robots.txt文件 ...

  4. Spring项目读取resource下的文件

    目录 一.前提条件 二.使用ClassPathResource类读取 2.1.Controller.service中使用ClassPathResource 2.2.单元测试使用ClassPathRes ...

  5. Java Struts图片上传至指定文件夹并显示图片

    继上一次利用Servlet实现图片上传,这次利用基于MVC的Struts框架,封装了Servlet并简化了JSP页面跳转. JSP上传页面 上传一定要为form加上enctype="mult ...

  6. Java 利用FTP上传,下载文件,遍历文件目录

    Java实现FTP上传下载文件的工具包有很多,这里我采用Java自带的API,实现FTP上传下载文件.另外JDK1.7以前的版本与其之后版本的API有了较大的改变了. 例如: JDK1.7之前 JDK ...

  7. java getResourcesAsStream()如何获取WEB-INF下的文件流

    getResourcesAsStream()来读取.properties文件,但是getResourcesAsStream()仅在java项目时能获取根目录的文件: 在web项目中,getResour ...

  8. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  9. UEditor Flash文件上传-crossdomain.xml文件配置

    在使用UEditor富文本时,如果客户端的浏览器是低版本浏览器,如IE7.IE8等,UEditor的文件上传方式将会使用flash方式上传而不是html5,而flash在跨域时唯一的限制策略就是cro ...

随机推荐

  1. Atitit   发帖机实现(3 )---usrQBN023 js提交ajax内容到后端规范与标准化

    Atitit   发帖机实现(3 )---usrQBN023 js提交ajax内容到后端规范与标准化 大段内容务必要替换转义换行符号1 提交务必使用utf编码,否则解码后的可能缺失,是web serv ...

  2. 发布iOS应用程序到苹果APP STORE完整流程

    参考:http://blog.csdn.net/mad1989/article/details/8167529(xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)) http ...

  3. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  4. 让AngularJS的$http 服务像jQuery.ajax()一样工作

    让AngularJS的$http 服务像jQuery.ajax()一样工作 $http的post . 请求默认的content-Type=application/json . 提交的是json对象的字 ...

  5. js相册展示

    自己写来用的,第一版草稿源码+效果图,功能优先,其他的再说,有时间再改进. <script src="http://common.cnblogs.com/script/jquery.j ...

  6. sql 循环处理表数据中当前行和上一行中某值相+/-

    曾经,sql中循环处理当前行数据和上一行数据浪费了我不少时间,学会后才发现如此容易,其实学问就是如此,难者不会,会者不难. 以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的 ...

  7. SAP Business One系统功能介绍

    SAP Business One(简称SAP B One)是一套价格合理.易于实施的综合业务管理解决方案.该解决方案专为中小型企业量身打造,可确保实现公司发展.提高可盈利性和控制力度以及实现业务流程的 ...

  8. Android开发之时间日期1

     对于手机的时间日期设置估计大家一定都不陌生吧,今天做了一个关于时间日期设置的小例子,其中遇到一个问题,求指导,如何使设置的时间日期和手机系统同步?还望高手指点一二. 先不说这个了,分享一下我的小例子 ...

  9. JAVA--网络编程(UDP)

    上午给大家简单介绍了一下TCP网络通信的知识,现在就为大家补充完整网络编程的知识,关于UDP的通信知识. UDP是一种不可靠的网络协议,那么还有什么使用价值或必要呢?其实不然,在有些情况下UDP协议可 ...

  10. (原创)详解KMP算法

    KMP算法应该是每一本<数据结构>书都会讲的,算是知名度最高的算法之一了,但很可惜,我大二那年压根就没看懂过~~~ 之后也在很多地方也都经常看到讲解KMP算法的文章,看久了好像也知道是怎么 ...