上传工具类java
来源:互联网 发布:.blog域名 编辑:程序博客网 时间:2024/04/29 21:26
1,在pom.xml中加入配置
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.1</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-math</artifactId><version>2.2</version></dependency>
2,在springmvc.xml中配置
<!-- 文件上传的配置 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- set the max upload size100MB --> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> <property name="defaultEncoding"> <value>utf-8</value> </property> </bean>
3, 上传时,记得在form中添加 method=”post” enctype=”multipart/form-data” 必须加
package com.util;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.UUID;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.multipart.MultipartFile;/** * 文件上传和下载工具类 * */public class UploadUtils {/** * @Description 文件上传: * 获得文件名称,文件保存路径,输入流,然后用流将他写入到具体的位置,即可完成文件上传 * @param request * @param file * @return 上传文件的保存路径 */public static String uploadFile(HttpServletRequest request,MultipartFile file) {// 目标一:获得原始文件名 String fileName = file.getOriginalFilename(); // 新文件名 (使用UUID生成新的文件名)//String newFileName = UUID.randomUUID() + fileName; String newFileName = fileName; // 获得项目的路径 ServletContext sc = request.getSession().getServletContext();// 目标二: 文件保存路径String path = sc.getRealPath("/img") + "\\";File f = new File(path); if (!f.exists()) f.mkdirs(); if (!file.isEmpty()) { try { FileOutputStream fos = new FileOutputStream(path + newFileName); //目标三: 输入流InputStream in = file.getInputStream(); int b = 0; while ((b = in.read()) != -1) { fos.write(b); } fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } return path + newFileName;} /** * @Description 文件下载: * 读取文件到流中,然后通过输出流输出 * @param filePath 完整的要下载的文件的路径 * @param fileName 文件的名称 * @param response 响应流 * @return 执行结果 * @throws UnsupportedEncodingException */public static boolean downloadFile(String filePath,String fileName,HttpServletResponse response) throws UnsupportedEncodingException {Boolean result = false;//目标一:将文件从电脑中读入到流中File file = new File(filePath);if (file.exists()) {response.setCharacterEncoding("UTF-8");//设置相应内容的编码格式response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(),"iso-8859-1"));byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {//目标一:将文件从电脑中读入到流中fis = new FileInputStream(file);bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {//目标二:通过输出流输出os.write(buffer, 0, i);i = bis.read(buffer);result = true;}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return result;}}
阅读全文
0 0
- 上传工具类java
- java上传文件工具类
- JAVA文件上传工具类
- Java一个文件上传工具类
- java上传图片剪切工具类
- java上传和下载文件工具类
- Java FTP上传文件工具类
- Java工具类之文件上传
- ftp上传工具类(java)
- Java SpringMVC文件上传工具类
- java工具类------pad文件上传
- Java中上传文件的工具类
- JAVA文件上传工具
- JAVA工具类(13)---图片上传工具类
- 上传工具类
- FTP上传工具类
- 文件上传工具类
- struts2上传工具类
- mysql 5.7.20, for Linux (x86_64) 配置远程连接 10061问题解决方案
- Tensorflow实战学习(二十四)【实现Softmax Regression(回归)识别手写数字】
- 对话框(3)-文件对话框
- jsp提示is exceeding the 65535 bytes limit错误
- SSO单点登录流程说明
- 上传工具类java
- navicat_premium for SQLServer 自带注册机
- Python 2/3 Input 区别
- 电路中电容和电感的作用
- Insight spring-task:executor
- C语言N进制推导
- 【BZOJ3625】【CF438E】小朋友和二叉树 NTT 生成函数 多项式开根 多项式求逆
- 系统设计和机器学习算法
- 11-EMM Procedure 1. Initial Attach Part2