急急急!!!异常 :java.io.FileNotFoundException: C:\fakepath\debug.txt (系统找不到指定的路径。)
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>写邮件</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<%-- 引入DWR相关脚本 --%>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<%-- 此处调用Java类对应的脚本,脚本名称一定要与dwr.xml文件配置的相应类的脚本名称相同 --%>
<script type="text/javascript" src="dwr/interface/registor.js"></script>
<script language="javascript" type="text/javascript">
function addemail(){
var recipients=document.getElementById("recipients").value;
var title=document.getElementById("title").value;
if(recipients == null||recipients=="" ){
alert("收件人不能为空!");
}else if(title == null||title=="" ){
alert("邮件标题不能为空!");
}else{
document.forms[0].submit();
}
}
<%-- 验证附件是否大于9M --%>
function regist(){
var filename = document.getElementById("accessory").value;
registor.isExist(filename, showResult);
}
function showResult(result){
if(result){
alert("附件附件上传成功");
}
else{
alert("附件不能大于9M!!!");
}
}
</script>
</head>
<body>
<%@ include file="login_check.jsp" %>
<form id="myForm" name="myForm" action="emailadd.action" method="post">
<input type="hidden" name="sender" value="${user.nickname}"/>
<input type="hidden" name="isread" value="未读"/>
<input type="hidden" name="isdeleted" value="0"/>
<div class="action">
<div class="t">
写邮件
</div>
<div class="pages">
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" width="30%">收件人:</td>
<td align="left" colspan="2">
<select id="recipients" name="recipients">
<option value="">请选择</option>
<c:forEach var="r" items="${recipientslist}">
<option value="${r}">${r}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td align="right" width="30%">邮件标题:</td>
<td align="left" colspan="2"><input type="text" name="title" id="title"/> <font color="#FF0000">*</font></td>
</tr>
<tr>
<td align="right" width="30%">邮件内容:</td>
<td align="left" colspan="2"><textarea cols="40" rows="10" name="content" id="content"></textarea></td>
</tr>
<tr>
<td align="right" width="30%">上传附件:</td>
<td align="left" colspan="2"><input type="file" name="accessory" id="accessory" onchange="regist()"/>
<font color="#FF0000">*</font>(上传附件不能大于9M)
</td>
</tr>
<tr>
<td align="center" colspan="2"><br/><input type="button" id="save" value="发送邮件" onclick="addemail()" /></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
以下是java代码:
package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 上传文件, Ajax判断文件大小
* @author Administrator
*
*/
public class Registor {
public boolean isExist(String filename){
boolean b = false;
File upload = new File(filename);
try{
if(new FileInputStream(upload).available()<= 1024*1024*9){
b = true;
}else{
b = false;
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return b;
}
}
当我点击“浏览”时,加入一个文件时,就弹出框框,文件也是小于9M的,这是控制台就报错了
java.io.FileNotFoundException: C:\fakepath\debug.txt (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.util.Registor.isExist(Registor.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
求高手指点 是不是我的代码写的不对呢?
[解决办法]
var recipients=document.getElementById("recipients").value;这个获取的也只是个文件名称,判断文件大小也应该在js中去判断。去百度下,应该有好多例子的。
[解决办法]
filename里面的文件路径中""这个应该替换成"\",才能识别。
filename = filename.replaceAll("","\");