Springmvc文件上传(servlet3.0)/下载(ssm)以及坑点

前言

(补充:再linux服务器上可能没用创建文件的权限,那就需要找到文件夹给权限,比如我的chmod -R 777 /home/tomcat/apache-tomcat-default/webapps/food)

  • 以前在servlet上写过文件上传和文件下载,最近由于需求需要集合到ssm中,有些坑点以前都忘记了。
  • 文件上传:文件上传首先要在xml中配置上传信息,ssm有强大的过滤功能,你不声明让某种类型进来他是进不来的,我是用的是servlet3.0的part上传文件,省掉依赖包。
    在web.xml中的springmvc配置中添加下面如下配置(配置方法可能很多,但是功能相同):

配置

  <servlet>
<servlet-name> springMvcservlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServletservlet-class>
  <init-param>
 <param-name>contextConfigLocationparam-name>
<param-value> classpath*:food-servlet.xmlparam-value>
 init-param>
<load-on-startup> 1load-on-startup>
 
  <multipart-config>
<max-file-size> 20971520max-file-size> 
<max-request-size>  20971520max-request-size> 
<file-size-threshold>  0file-size-threshold> 
 multipart-config>
 servlet>

代码

下面是我的下载的模板:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.food.dao.foodMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.food.bean.path;
@Controller
public class uploadController {
 @Autowired(required = true)
 private foodMapper foodmapper;
 @RequestMapping(value="/onfile")
 public String uploadfile(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
 {
 HttpSession session=request.getSession();
 request.setCharacterEncoding("UTF-8");//
 response.setCharacterEncoding("UTF-8");
 Part part=request.getPart("file");
 upload(part,session);
 return "sucess";

 }
 public String getname(Part part)//获取文件名
 {
 String contentdisposition=part.getHeader("content-disposition");//form-data; name="file"; filename="jspѧϰ.txt"
 String[]filename=contentdisposition.split("=");//

 String filename1=filename[filename.length-1];//"jspѧϰ.txt"
 String filename2=filename1.replace("\"", "");
 return filename2;
 }
 public void upload(Part part, HttpSession session) throws IOException
 {

 String path=session.getServletContext().getRealPath("food/");
 //System.out.println(path);
 String filename=getname(part);
 File file=new File(path);
 if(!file.exists())//
 {
 file.mkdirs();
 }
 File file2=new File(file,filename);//
 if(!file2.exists()){file2.createNewFile();}
 InputStream in=part.getInputStream();
 OutputStream out=new FileOutputStream(file2);
 BufferedInputStream buf=new BufferedInputStream(in);
 BufferedOutputStream bufout=new BufferedOutputStream(out);
 byte by[]=new byte[1024*10];
 int q=0;
 while((q=buf.read(by))!=-1)
 {
 bufout.write(by);
 //by=new byte[1024]

 }
 bufout.close();
 buf.close();
 in.close();
 out.close();
 }

}

这个是我的一个小项目剪出来的下载模板,如果有问题修改修改就可以使用,或者可以私信我。
文件下载:文件上传使用的是传统的模板,有用sringmvc的方法的可以
文件下载的一个大坑:当时下载中文名成发现中文名变成"–"无法显示中文,怎么改编码都没有用,后来看了百度说是tomcat的默认编码is编码问题,解决方法和get请求有时的乱码解决很类似,字符串重新编码。核心是:new String(filename.getBytes(“utf-8”),“ISO8859_1”));

@RequestMapping(value = "/download")
 public String download(String filename,String path,HttpServletRequest request, HttpServletResponse response) throws IOException {
 System.out.println(filename);
 response.setCharacterEncoding("utf-8");
 request.setCharacterEncoding("UTF-8");
 HttpSession session=request.getSession();
 response.setContentType("text/html");
 System.out.println(filename);
 //设置文件MIME类型
 response.setContentType(session.getServletContext().getMimeType(filename));
 //设置Content-Disposition
 response.setHeader("Content-Disposition", "attachment;filename=" new String(filename.getBytes("utf-8"),"ISO8859_1"));
 String fullFileName = session.getServletContext().getRealPath( path); //获取绝对路径
 System.out.println(fullFileName);
 InputStream in = new FileInputStream(fullFileName);
 BufferedInputStream buf=new BufferedInputStream(in);
 OutputStream out = response.getOutputStream();
 BufferedOutputStream ou=new BufferedOutputStream(out);
 //PrintWriter out = response.getWriter();
 int b=0;
 byte[]bite=new byte[1024];
 while((b=buf.read(bite))!=-1)
 {
 ou.write(bite);
 }
 ou.close();
 buf.close();
 in.close();
 out.close();
 return null;
 }

文件上传和下载一定要用buffer类,速度差别如果带宽能够满足的话差别还是很大的。

如果对后端、爬虫、数据结构算法等感性趣欢迎关注我的个人公众号交流:bigsai

你可能感兴趣的:(#,java,EE,#,javaWeb)