java的文件上传工具类

package util;


import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import javax.imageio.ImageIO;


import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;


import sun.misc.BASE64Decoder;


/**
 * 上传文件
 * 
 */
public class FileUpload {


/**
* @param file
*            //文件对象
* @param filePath
*            //上传路径
* @param fileName
*            //文件名
* @return 文件名
*/
public static String fileUp(MultipartFile file, String filePath, String fileName) {
String extName = ""; // 扩展名格式:

File file2 = new File(filePath);
// 如果文件夹不存在则创建
if (!file2.exists() && !file2.isDirectory()) {
System.out.println(filePath + "//不存在");
file2.mkdir();
}


try {
if (file.getOriginalFilename().lastIndexOf(".") >= 0) {
extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
}
copyFile(file.getInputStream(), filePath, fileName + extName).replaceAll("-", "");
} catch (IOException e) {
System.out.println(e);
}
return fileName + extName;
}


public static String fileUpV2(String file, String filePath, String fileName) {
String extName = ".png"; // 扩展名格式:
try {


BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] b = decoder.decodeBuffer(file);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
InputStream inputStream = FileUpload.byte2Input(b);
copyFile(inputStream, filePath, fileName + extName).replaceAll("-", "");
} catch (IOException e) {
System.out.println(e);
}
return fileName + extName;
}


public static InputStream fileUpV3(String file) {
try {


BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] b = decoder.decodeBuffer(file);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}


InputStream inputStream = new ByteArrayInputStream(b);
return inputStream;
} catch (IOException e) {
System.out.println(e);
}
return null;
}


/**
* 处理webp processWebp 处理webp (这里描述这个方法适用条件 – 可选) 标记:@param inputStream 标记:@param
* file2 标记:@return 返回值:File 作者:brandon

* @exception @since
*                1.0.0
*/
public static File processWebp(InputStream inputStream, String file2) {
try {
BufferedImage im = ImageIO.read(inputStream);
File file = new File(file2);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
ImageIO.write(im, "webp", file);
return file;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}


/**

* processPng(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) 标记:@param inputStream
* 标记:@param file2 标记:@return 返回值:File 作者:brandon

* @exception @since
*                1.0.0
*/
public static File processPng(InputStream inputStream, String file2) {
try {
BufferedImage im = ImageIO.read(inputStream);
File file = new File(file2);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
ImageIO.write(im, "png", file);
return file;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}


/**

* processPng(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) 标记:@param im 标记:@param file2
* 标记:@return 返回值:File 作者:brandon

* @exception @since
*                1.0.0
*/
public static File processPng(BufferedImage im, String file2) {
try {
File file = new File(file2);
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
ImageIO.write(im, "png", file);
return file;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}


/**

* byte2Input(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) 标记:@param buf 标记:@return
* 返回值:InputStream 作者:brandon

* @exception @since
*                1.0.0
*/
public static final InputStream byte2Input(byte[] buf) {
return new ByteArrayInputStream(buf);
}


/**
* 写文件到当前目录的upload目录中

* @param in
* @param fileName
* @throws IOException
*/
private static String copyFile(InputStream in, String dir, String realName) throws IOException {
File file = new File(dir, realName);
System.out.println("copy file before================= " + dir);
if (!file.exists()) {
System.out.println("copy file path not exist================= " + dir);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
FileUtils.copyInputStreamToFile(in, file);
return realName;
}


public static InputStream returnBitMap(String path) {
URL url = null;
InputStream is = null;
try {
url = new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 利用HttpURLConnection对象,我们可以从网络中获取网页数据.
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream(); // 得到网络返回的输入流


} catch (IOException e) {
e.printStackTrace();
}
return is;
}


/**

* fileUpReturnFile(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) 标记:@param file
* 标记:@param filePath 标记:@param fileName 标记:@return 返回值:File 作者:brandon

* @exception @since
*                1.0.0
*/
public static File fileUpReturnFile(MultipartFile file, String filePath, String fileName) {
String extName = ""; // 扩展名格式:
File result_file = null;
try {
if (file.getOriginalFilename().lastIndexOf(".") >= 0) {
extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
}
result_file = copyFileReturnFile(file.getInputStream(), filePath, fileName + extName);
} catch (IOException e) {
System.out.println(e);
}
return result_file;
}


/**

* copyFileReturnFile(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) 标记:@param in
* 标记:@param dir 标记:@param realName 标记:@return 标记:@throws IOException 返回值:File
* 作者:brandon

* @exception @since
*                1.0.0
*/
private static File copyFileReturnFile(InputStream in, String dir, String realName) throws IOException {
File file = new File(dir, realName);
System.out.println("copy file before================= " + dir);
if (!file.exists()) {
System.out.println("copy file path not exist================= " + dir);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
FileUtils.copyInputStreamToFile(in, file);
return file;
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值