JAVA上传文件工具类

文件上传工具类

  • UploadFileUtils 工具类

UploadFileUtils 工具类

package com.example.newhp.util;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;import org.apache.tomcat.util.http.fileupload.FileItem;
import org.springframework.web.multipart.MultipartFile;public class UploadFileUtils {//  保存文件public synchronized static HashMap uploadFile(MultipartFile fileItem, String path, String secondPath) throws IOException {InputStream is=null;FileOutputStream fos=null;    HashMap mp=new HashMap();try {//上传的是文件,获得文件上传字段中的文件名//注意IE或FireFox中获取的文件名是不一样的,IE中是绝对路径,FireFox中只是文件名。String fileName=fileItem.getName();System.out.println(fileName);// 文件类型/*   String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()): null;*/// 文件将保存路径String filePath =secondPath+getDateDir();String newPath = path+filePath;//文件名使用UUID和原文件名组成的// 为避免原文件名存在中文,新文件名只使用UUID作为新名称String newFileName= UUID.randomUUID().toString().replaceAll("-", "")+".pdf";// 判断路径是否存在,如果不存在就创建一个File filepath = new File(newPath, newFileName);if (!filepath.getParentFile().exists())filepath.getParentFile().mkdirs();is=fileItem.getInputStream();fos=new FileOutputStream(newPath+newFileName);byte[] buff=new byte[1024];int len=0;while((len=is.read(buff))>0){fos.write(buff);}mp.put("fileName",newFileName);mp.put("filePath",filePath);} catch (Exception e) {// TODO: handle exception}finally{is.close();fos.close();}	return mp;}/*** 生成以时间作为文件夹路径* @return*/public static String getDateDir() {SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 sdf.applyPattern("yyyyMM/dd/");// a为am/pm的标记  Date date = new Date();// 获取当前时间 return sdf.format(date);}/*** 更换pdf报告* @return* @zpath=(更换文件,路径,盘符+文件夹+文件名)*/public static Integer ghFile(MultipartFile file,String zpath) {BufferedInputStream in=null;BufferedOutputStream out=null;System.err.println(zpath);try {File fileold = new File(zpath);if(fileold.exists()){fileold.delete();}in = new BufferedInputStream(file.getInputStream());// 获取输出流out = new BufferedOutputStream(new FileOutputStream(zpath));// 读数据byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组int n;// 每次读取到的字节数组的长度while ((n = in.read(bb)) != -1) {out.write(bb, 0, n);// 写入到输出流}out.flush();out.close();// 关闭流in.close();return 1;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();return 0;}}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部