JAVA Excel文件上传,上传后的Excel文件打开时报有不可读取的内容,怎么回事? 10
StringrootPath;DataInputStreamin=null;FileOutputStreamfileOut=null;StringremoteAddr=r...
String rootPath;
DataInputStream in = null;
FileOutputStream fileOut = null;
String remoteAddr = request.getRemoteAddr();
String serverName = request.getServerName();
String realPath = request.getRealPath(serverName);
realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath = realPath + "\\upload\\";
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data") >= 0){
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength > MAX_SIZE){
out.println("上传的文件字节数不可以超过" + MAX_SIZE );
return;
}
String file = new String(dataBytes);
//out.println(file);File checkFile = new File(fileName);
if(checkFile.exists()){
out.println("文件已经存在");
}
File fileDir = new File(rootPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
fileOut = new FileOutputStream(fileName);
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.close(); 展开
DataInputStream in = null;
FileOutputStream fileOut = null;
String remoteAddr = request.getRemoteAddr();
String serverName = request.getServerName();
String realPath = request.getRealPath(serverName);
realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath = realPath + "\\upload\\";
String contentType = request.getContentType();
try{
if(contentType.indexOf("multipart/form-data") >= 0){
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
if(formDataLength > MAX_SIZE){
out.println("上传的文件字节数不可以超过" + MAX_SIZE );
return;
}
String file = new String(dataBytes);
//out.println(file);File checkFile = new File(fileName);
if(checkFile.exists()){
out.println("文件已经存在");
}
File fileDir = new File(rootPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
fileOut = new FileOutputStream(fileName);
fileOut.write(dataBytes,startPos,(endPos - startPos));
fileOut.close(); 展开
1个回答
2015-09-09
展开全部
这样接收,当然岩丛不行了,,,,,,使用smartupload等接收上传,或Struts接收上传的文件
文件上传,要浏览器端编码的 <form action="UploadServlet" method="post"链茄 enctype="multipart/粗唤樱form-data">
文件上传,要浏览器端编码的 <form action="UploadServlet" method="post"链茄 enctype="multipart/粗唤樱form-data">
追问
你好,感谢你回答!问题主要是前台请求只能用request.getInputStream()接受,这样上传文件怎么做啊,求支援,,,
追答
上面回答中的第1行的后面,提出的解决办法,你应该先看看、试试。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询