java web上传,java web文件上传解决办法

Java codepublic synchronized ActionForward add(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

NpvidoForm npvidoForm = (NpvidoForm) form;// TODO Auto-generated method

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

npvidoForm.getNpvido().setPostTime(timestamp);

// 文件大小

// ------------------------------------------------//

PersonInfo personInfo1 = AuserinfoUtil.getPersonInfo(request, response);

try {

npvidoForm.getNpvido().setZtname((int)npvidoForm.getFile().getFileSize()/1024+"");

uploadId = personInfo1.getId();

allCount = npvidoForm.getFile().getFileSize();

// 限制文件大小为100M以内

if (npvidoForm.getFile().getFileSize() < 102400000) {

String fileName = npvidoForm.getFile().getFileName();

if (fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length()).equals("flv")) {

String systemFileName = System.currentTimeMillis()+ fileName.substring(fileName.lastIndexOf("."),fileName.length());

DataInputStream inputStream = new DataInputStream(npvidoForm.getFile().getInputStream());

// 服务器的位置

String path = request.getContextPath();

String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";

String pathString = request.getRealPath("/")+ "UserFiles\\video\\";

File file = new File(pathString);

if (!file.exists())

file.mkdirs();

// 文件实际上传地址

String uploadPath = pathString + systemFileName;

// 存储位置

npvidoForm.getNpvido().setPicUrl(basePath+"UserFiles\\video\\" + systemFileName);

System.out.println("上传的路径:"+uploadPath);

System.out.println("数据库保存的路径:"+basePath+"UserFiles\\video\\" + systemFileName);

// 上传

DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(uploadPath)));

dataOutputStream.flush();

int b = -1;

while ((b = inputStream.read()) != -1) {

dataOutputStream.write(b);

//为了做简易的进度条采用的获取当前已上传的文件的大小

File file2 = new File(uploadPath);

nowCount = (int) file2.length();

}

inputStream.close();

dataOutputStream.close();

} else {

return new ActionForward("findAll","/papersite/channel/paper/addNpvido.jsp", false);

}

} else {

request.setAttribute("uploadSize", "0");

return new ActionForward("findAll","/papersite/channel/paper/addNpvido.jsp", false);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// ------------------------------------------------//

npvidoForm.getNpvido().setComeFrom(personInfo1.getAccount());

npvidoService.save(npvidoForm.getNpvido());

nowCount = 0;

allCount = 0;

averageCount = 0;

uploadId = 0;

// log

PersonInfo personInfo = AuserinfoUtil.getPersonInfo(request, response);

npvidoService.logAdd(personInfo.getId(), "视频管理", "添加", "添加ID:"

+ npvidoForm.getNpvido().getId(), request);

return new ActionForward("findAll", "/npvido.do?action=findAll", false);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值