java web文件上传
4G的文件 以web的方式怎么上传。。。
[解决办法]
boolean flag = true; int length = getMyfileFileName().split("\\.").length; String docType = getMyfileFileName().split("\\.")[length - 1];// 文件类型 try{//以下判断文件大小的 FileInputStream fiss = new FileInputStream(myfile); String filesz = String.valueOf(fiss.available()/1000) ;//2000000k===2G if(Long.parseLong(filesz)>2000000l) { try { response.getWriter().println("<script>alert('上传文件不能超过2G!');window.parent.location.reload();</script>"); } catch (IOException e) { e.printStackTrace(); } return null ; } }catch(Exception e1){ System.out.println("我的文档上传出错!"); } // 以下才是正常的文件上传 if (flag) { String currentTime = (new SimpleDateFormat("yyyyMMdd")) .format(new Date());// 把当前时间当成文件夹名字 File dirFile = new File(ServletActionContext.getServletContext() .getRealPath("/") + "upload\\document\\" + currentTime); if (!dirFile.exists()) { dirFile.mkdir(); } String docTitle = request.getParameter("docTitle");// 文件名 String docDescription = request.getParameter("docDescription");// 文件描述 String docPath = "upload\\document\\" + currentTime + "\\" + getMyfileFileName();// 文件路径 // 以服务器的文件保存地址和原文件名建立上传文件输出流 File imageFile = new File(ServletActionContext .getServletContext().getRealPath("/") + "upload\\document\\" + currentTime + "\\" + getMyfileFileName()); copy(myfile, imageFile); FsDocument fd = new FsDocument(); fd.setUserId(userId);// 用户ID-----现在是死的··以后还得动态得到当前用户 fd.setDocType(docType);// 文档类型 fd.setDocTitle(docTitle);// 文档标题 fd.setDocPath(docPath);// 文档路径 fd.setDocId(0l);// 文档ID fd.setDocDescription(docDescription);// 文档描述 fd.setCreTime(new Date());// 创建时间 documentService.saveData(fd); } try { response .getWriter() .println( "<script>alert('文件上传成功!');window.parent.location.reload();</script>"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void copy(File src, File dst) { try { InputStream in = null ; OutputStream out = null ; try { in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE); byte [] buffer = new byte [BUFFER_SIZE]; while (in.read(buffer) > 0 ) { out.write(buffer); } } finally { if ( null != in) { in.close(); } if ( null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } }
[解决办法]
public 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); }
[解决办法]
4G太大了
如果要使用可以考虑Struts中的文件上传
java文件
package label;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;
public class UploadFileAction extends ActionSupport {
private File XYZ;
private String FileName;
public File getXYZ() {
return XYZ;
}
public void setXYZ(File xYZ) {
XYZ = xYZ;
}
public String getXYZFileName() {
return FileName;
}
public void setXYZFileName(String fileName) {
FileName = fileName;
}
@Override
public String execute() {
try {
FileInputStream fileInput = new FileInputStream(XYZ);
FileOutputStream fileOutput = new FileOutputStream(new File("D:/"
+ FileName));
byte[] b = new byte[1024];
int length = 0;
try {
while ((length = fileInput.read(b)) != -1) {
fileOutput.write(b, 0, length);
fileOutput.flush();
}
fileOutput.close();
fileInput.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "suessed";
}
}
页面:JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'LoginTwo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action ="labelLoginTwo">
<s:submit value = "提交"/>
</s:form>
</body>
</html>
Strut.xml文件
<action name="userLogin" class="label.UploadFileAction">
<result name="suessed">/label/Result.jsp</result>
</action>
[解决办法]
32位Windows操作系统下单个进程用户模式内存访问的限制是2G,Struts的文件上传是将文件读入内存,超过2G无法上传