java web 文件上传失败_JavaWeb 错误登记簿(文件上传)

import java.io.File;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadHelper {

public static Map upload(HttpServletRequest request, long max, String allowtype) throws Exception {

Map map = new HashMap();

request.setCharacterEncoding("utf-8");

if (ServletFileUpload.isMultipartContent(request)) {

DiskFileItemFactory fty = new DiskFileItemFactory();

// fty.setRepository(new File("D:\\uploadtmp"));// fty.setSizeThreshold(4*1024);ServletFileUpload upload = new ServletFileUpload(fty);// 上传核心组件if (max > 0)

upload.setSizeMax(max);

List items = upload.parseRequest(request);// 分解上传上来的东西for (FileItem item : items) {

if (item.isFormField())

map.put(item.getFieldName(), item.getString("utf-8"));

else {

String fname = item.getName();

if (fname == null || fname.isEmpty())

continue;

String ext = fname.substring(fname.lastIndexOf('.') + 1);// 那他的后缀名if (allowtype != null && allowtype.indexOf(ext) == -1)

throw new RuntimeException("不符合上传格式");

byte[] bytes = item.get();

item.delete();

map.put(item.getFieldName(), bytes);// 返回二进制数组map.put("filename", fname);

}

}

}

return map;

}

public static Map upload(HttpServletRequest request, String dirpath// 文件保存到哪里去, int max// 不能超过多大, String allowtype// 允许什么类型) throws Exception {

Map map = new HashMap();

String dir = request.getSession().getServletContext().getRealPath(dirpath); // /imgs-->物理路径request.setCharacterEncoding("utf-8");

if (ServletFileUpload.isMultipartContent(request)) {

DiskFileItemFactory fty = new DiskFileItemFactory();

// fty.setRepository(new File("D:\\uploadtmp"));// fty.setSizeThreshold(4*1024);ServletFileUpload upload = new ServletFileUpload(fty);// 上传核心组件upload.setSizeMax(max);// 设置最大大小List items = upload.parseRequest(request);// 分解上传上来的东西for (FileItem item : items) {

if (item.isFormField())

map.put(item.getFieldName(), item.getString("utf-8"));

else {

String fname = item.getName();

if (fname == null || fname.isEmpty())

continue;

String ext = fname.substring(fname.lastIndexOf('.') + 1);// 那他的后缀名if (allowtype.indexOf(ext) == -1)

throw new RuntimeException("不符合图片格式");

String filename = new Date().getTime() + "." + ext;

item.write(new File(dir + "/" + filename));

byte[] imgByte = item.get();

item.delete();// 清空临时文件夹(过渡用的)中的数据map.put(item.getFieldName(), filename);// 把处理好的文件名放字典,后面要用到}

}

}

return map;

}

public static Map upload2(HttpServletRequest request, String dir// 文件保存的绝对路径, int max// 不能超过多大, String allowtype// 允许什么类型) throws Exception {

Map map = new HashMap();

request.setCharacterEncoding("utf-8");

if (ServletFileUpload.isMultipartContent(request))// 校验一下是不是对应的格式{

DiskFileItemFactory fty = new DiskFileItemFactory();

// fty.setRepository(new File("D:\\uploadtmp"));// fty.setSizeThreshold(4*1024);ServletFileUpload upload = new ServletFileUpload(fty);// 上传核心组件upload.setSizeMax(max);// 设置最大大小List items = upload.parseRequest(request);// 分解上传上来的东西for (FileItem item : items) {

if (item.isFormField())// 是不是普通的文本,等效于request.getParameter("")map.put(item.getFieldName(), item.getString("utf-8"));

else {

String fname = item.getName();

if (fname == null || fname.isEmpty())

continue;

String ext = fname.substring(fname.lastIndexOf('.') + 1);// 拿他的后缀名.jpg,.pngif (allowtype != null && allowtype.indexOf(ext) == -1)

throw new RuntimeException("不符合图片格式");

String filename = new Date().getTime() + "." + ext;

item.write(new File(dir + "/" + filename));

// byte[] imgByte=item.get();item.delete();// 清空临时文件夹(过渡用的)中的数据map.put(item.getFieldName(), filename);// 把处理好的文件名放字典,后面要用到}

}

}

return map;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值