亿速云服务器 如何上传文件,使用MultipartFile怎么实现一个文件上传功能

使用MultipartFile怎么实现一个文件上传功能

发布时间:2021-01-20 16:43:15

来源:亿速云

阅读:139

作者:Leah

使用MultipartFile怎么实现一个文件上传功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一.主要有两个java类,和一般的servlet放在一起即可.

1.FileUploadBean.javapackage chb.demo.web;

import org.springframework.web.multipart.MultipartFile;

/**

* @author chb

*

*/

public class FileUploadBean {

private MultipartFile file;

public void setFile(MultipartFile file) {

this.file = file;

}

public MultipartFile getFile() {

return file;

}

}

2.FileUploadController.javapackage chb.demo.web;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.SimpleFormController;

/**

* @author chb

*

*/

public class FileUploadController extends SimpleFormController {

protected ModelAndView onSubmit(

HttpServletRequest request,

HttpServletResponse response,

Object command,

BindException errors){

try

{

// cast the bean

FileUploadBean bean = (FileUploadBean) command;

// let's see if there's content there

MultipartFile file = bean.getFile();

if (file == null) {

throw new Exception("上传失败:文件为�空");

}

if(file.getSize()>10000000)

{

throw new Exception("上传失败:文件大小不能超过10M");

}

//得到文件�名

String filename=file.getOriginalFilename();

if(file.getSize()>0){

try {

SaveFileFromInputStream(file.getInputStream(),"D:/",filename);

} catch (IOException e) {

System.out.println(e.getMessage());

return null;

}

}

else{

throw new Exception("上传失败:上传文件不能为�空");

}

// well, let's do nothing with the bean for now and return:

try {

return super.onSubmit(request, response, command, errors);

} catch (Exception e) {

System.out.println(e.getMessage());

return null;

}

}

catch(Exception ex)

{

System.out.println(ex.getMessage());

return null;

}

}

/**保存文件

* @param stream

* @param path

* @param filename

* @throws IOException

*/

public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException

{

FileOutputStream fs=new FileOutputStream( path + "/"+ filename);

byte[] buffer =new byte[1024*1024];

int bytesum = 0;

int byteread = 0;

while ((byteread=stream.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

fs.flush();

}

fs.close();

stream.close();

}

}

二.配置文件中如下配置:

1.web.xml,利用spring mvc模式,大家应该都很熟悉了

chb

org.springframework.web.servlet.DispatcherServlet

1

chb

*.do

2.chb-servlet.xml,这里要配置映射,并可以设定最大可上传文件的大小<?xml  version="1.0" encoding="UTF-8"?>

beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

action

index

fileUploadController

三.设定jsp页面

上传文件:

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值