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<byte[]> 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<byte[]>(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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="http://blog.163.com/xiao_long/styles.css">
-->
</head>

<body>
<h1>Please upload a file</h1>
<form method="post" action="form.action" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
<a href="http://blog.163.com/xiao_long/download.action">下载</a>
</body>
</html>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<context:annotation-config />

<context:component-scan base-package="com.julongtech" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 文件的处理 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000" />
</bean>


</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值