SpringBoot上传文件

定义实体类

package com.test.model;

import java.io.Serializable;
import java.sql.Date;

import javax.sql.rowset.serial.SerialBlob;

public class FileInfo implements Serializable{
	private Integer id = null;
	private String name = null;
	private String contextType = null;
	private Long length = null;
	private Date dt = null;
	private byte[] content = null;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContextType() {
		return contextType;
	}
	public void setContextType(String contextType) {
		this.contextType = contextType;
	}
	public Long getLength() {
		return length;
	}
	public void setLength(Long length) {
		this.length = length;
	}
	public Date getDt() {
		return dt;
	}
	public void setDt(Date dt) {
		this.dt = dt;
	}
	public byte[] getContent() {
		return content;
	}
	public void setContent(byte[] content) {
		this.content = content;
	}


}

添加Mapper.xml

	<insert id="saveFile" parameterType="com.test.model.FileInfo"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		insert into T_FILE(name,contexttype,length,dt,content) values (#{name},#{contextType},
			#{length},#{dt},#{content,jdbcType=BLOB})
	</insert>
      
	<select id="getFileById" resultType="com.test.model.FileInfo">
		select * from T_FILE where id=#{id}
	</select>

Controller控制

	public FileInfo uploadFile(HttpServletRequest req)
	{
		if(!(req instanceof MultipartHttpServletRequest))
			return null;
		MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
		MultipartFile multipartFile = mreq.getFile("upfile");
		try
		{
			InputStream is = multipartFile.getInputStream();
			byte[] bytes = FileCopyUtils.copyToByteArray(is);
			FileInfo fi = new FileInfo();
			fi.setName(multipartFile.getName());
			fi.setContextType(multipartFile.getContentType());
			fi.setLength(new Long(multipartFile.getBytes().length));
			fi.setContent(bytes);
			orgmodel.saveFile(fi);
			return fi;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

下载Servlet

package com.test.util;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.test.model.FileInfo;
import com.test.service.IOrgModel;

@WebServlet(urlPatterns = "/downfile/*")
public class DownFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Autowired
	private IOrgModel orgmodel;
       
    public DownFileServlet() {
        super();
    }

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try
		{
			String id = req.getParameter("id");
			FileInfo fi = orgmodel.getFileById(new Integer(id));
			resp.setCharacterEncoding("utf-8");
			resp.setContentType(fi.getContextType());
			resp.setHeader("Content-Disposition","attachment; filename="+fi.getName()+"");
			ServletOutputStream  out =resp.getOutputStream(); 
			out.write(fi.getContent());
			out.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}

页面

	function savewin()
	{
		var isok = $('#frm').form('validate');
		if(!isok)
		{
			$.messager.alert('警告','数据校验失败'); 
			return;
		}
		
		$('#frm').form('submit', {    
		    url:'/usersave',    
		    onSubmit: function(){    
  
		    },    
		    success:function(data){    
				$('#win').window('close');
				$('#dg').datagrid("reload");
		    }    
		}); 

	}
	
<div id="win" class="easyui-window" title="用户维护" style="width:600px;height:500px"   
	        data-options="iconCls:'icon-save',modal:true">
		<form id="frm" action="/usersave" method="POST" enctype="multipart/form-data">
			<input type="hidden" id="id" name="id" value=""/>
			<input type="hidden" id="imgId" name="imgId" value=""/>
		    <div style="margin-left:20px;margin-top:10px">
		    	<input class="easyui-textbox" id="loginId" name="loginId" value="" style="width:70%" data-options="required:true,label:'登录名',multiline:false">
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
		    	<input class="easyui-textbox" id="name" name="name" value="" style="width:70%" data-options="label:'姓名',multiline:false,required:true">
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
		    	<input type="password" class="easyui-textbox" id="pwd" name="pwd" value="" style="width:70%" data-options="label:'密码',validType:'password',multiline:false,required:true">
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
		    	<input class="easyui-datebox" id="birthday" name="birthday" value="" style="width:70%" data-options="label:'生日',validType:'password',multiline:false,validType:'date'">
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
		    	<input class="easyui-textbox" id="telNo" name="telNo" value="" style="width:70%" data-options="label:'电话',multiline:false">
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
		    	<input class="easyui-filebox" id="upfile" name="upfile" value="" style="width:70%" data-options="label:'上传头像'">
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
		    	<div id="userheader"></div>
		    </div>
		    <div style="margin-left:20px;margin-top:10px">
				<select id="status" name="status" class="easyui-combobox" style="width:200px;" data-options="label:'状态'">   
				    <option value="0">正常</option>   
	 				<option value="1">删除</option>
	 				<option value="2">锁定</option>
				</select> 
		    </div>
		    <div style="margin-left:20px;margin-top:20px">
				<select id="oid" name="oid" class="easyui-combobox" style="width:200px;" 
					url="/orgload" data-options="label:'所在部门',valueField:'id',textField:'name'">   
				</select> 
		    </div>
		    <div style="margin-left:20px;margin-top:20px">
				<a id="btn" onclick="savewin()" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保存</a>
				<a id="btn" onclick="closewin()" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">关闭</a>
			</div>
		</form>
	</div>

启动类

需要扫描Servlet
@ServletComponentScan(“com.test.util”)

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;


@SpringBootApplication
@ServletComponentScan("com.test.util")
public class Starter 
{
    public static void main( String[] args )
    {
        SpringApplication.run(Starter.class,args);
    }
}

SQL

create table ORGMODEL_USER (id int auto_increment primary key,
	loginid varchar(100),name varchar(200),pwd varchar(100),
	birthday date,telno varchar(100),email varchar(100),
	status int,oid int,imgid int);

create table T_FILE(id int auto_increment primary key,
	name varchar(200),contexttype varchar(100),length bigint,dt date,content blob);

上传文件大小限制

Mysql数据库字段类型修改
  MySQL的四种BLOB类型
  类型 大小(单位:字节)
  TinyBlob 最大 255
  Blob 最大 65K
  MediumBlob 最大 16M
  LongBlob 最大 4G

1.修改数据库字段
alter table t_file modify content mediumBlob;

2.SpringBoot环境设置上传文件限制

修改启动类

@Bean  
public MultipartConfigElement multipartConfigElement() {  
    MultipartConfigFactory factory = new MultipartConfigFactory();  
    //单个文件最大  
    factory.setMaxFileSize("10240KB"); //KB,MB  
    /// 设置总上传数据总大小  
    factory.setMaxRequestSize("102400KB");  
    return factory.createMultipartConfig();  
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值