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);
}