Spring MVC文件上传 下载

最近在做 文件的上传和下载 看了好多的方法,网上各种各样的 既然spring这么强大 觉得上传组件还是有的吧
捣腾了一下午 时间

需要的jar 我用的spring3.2 版本  官方下载除了tomcat struts相关的不要 就可以了 全部拷贝进去
  • org.springframework.aop

  • org.springframework.beans

  • org.springframework.context

  • org.springframework.context.support

  • org.springframework.expression

  • org.springframework.jdbc

  • org.springframework.jms

  • org.springframework.orm

  • org.springframework.oxm

  • org.springframework.test

  • org.springframework.transaction

  • org.springframework.web

  • org.springframework.web.portlet

  • org.springframework.web.servlet

还有两个公共commons

commons-fileupload-1.3.1.jar
commons-io-2.5.jar
commons-logging-1.2.jar

接下来看看action的写法
 
   

package com.julongtech;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class TestAction {

/**
* 文件上传
* @param name
* @param file
*/
@RequestMapping(value = "form", method = RequestMethod.POST)
public void handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
//byte[] bytes = file.getBytes();
//获取原始的文件名称
String n = file.getOriginalFilename() ;
//路径和文件名
String uri = "C:\\123.txt";
File files = new File(uri);
file.transferTo(files);
System.out.println("文件写入成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


/*下载*/
@RequestMapping("download")
public ResponseEntity download() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "dict.txt");
String uri = "C:\\123.txt";
File files = new File(uri);
byte[] bytes = FileUtils.readFileToByteArray(files);
return new ResponseEntity(bytes,headers, HttpStatus.OK);
}

}

jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>





My JSP 'index.jsp' starting page









Please upload a file







下载



使用spring的上传下载需要设置一下配置文件
spring-servlet.xml
 
   


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">



class="org.springframework.web.servlet.view.InternalResourceViewResolver">




class="org.springframework.web.multipart.commons.CommonsMultipartResolver">








你可能感兴趣的:(Spring)