struts上传文件 文件空指针,控制台没报错。。
会先显示上传成功的界面,然后就报这个异常,文件上传 失败。
空指针报的是文件输出流。,谁帮我解决了,再加50分,,弄了好久了。
这个是浏览器上报的错。。,我发现了,action里面的execute方法竟然会执行两遍,,我写了个输出语句System.out.println(fileName);第一次是test.txt,第二次是null,,.找不到原因会执行两遍。
[解决办法]
action配置的地方,和action调用的地方发来看看啊
[解决办法]
如果是提交2次的问题,检查下你jsp页面是否有<img src="#">,或者是其它包含#的地方,把这些代码去掉试下就行了,有些浏览器会有这种情况。
[解决办法]
下面是我写的一个案例,LZ看下能否帮到你
JSP
<div id="fileId" style='display:none;' class="file-box">
<form id="form2" method="post" enctype="multipart/form-data">
<input type='text' name='textfield' id='textfield' class='txt' />
<input type='button' class='btn' value='浏览...' />
<input type="file" name="myfile" class="file" id="myfile"
size="28" onchange="document.getElementById('textfield').value=this.value" />
<input type="button" name="button" class="btn" id="btnSubmit" onclick="uploadExcel()" value="上传" />
</form>
</div>
JS
function uploadExcel() {
var fileValue = document.getElementById("myfile").value;
var length=fileValue.length;
var xls = fileValue.substring(length-3,length);
if(fileValue != ""){
if(xls != "xls"){
alert("请选择正确的文件格式:如(test.xls)");
var obj = document.getElementById('myfile') ;
obj.outerHTML=obj.outerHTML;
document.getElementById("textfield").value = "";
} else {
$(document).ready(function() {
var options = {
url : getRootPath()+"/report/Upload.action",
type : "POST",//提交方式
dataType : "script",//数据类型
success : function(msg) {//调用Action后返回过来的数据
alert("文件上传成功");
$("#search").hide();
$("#gridShow").show();
loadDimensionsTable(5);
var obj = document.getElementById('myfile') ;
obj.outerHTML=obj.outerHTML;
document.getElementById("textfield").value = "";
}
};
$("#form2").ajaxSubmit(options);//绑定页面中form表单的id
return false;
});
}
} else {
alert('请选择上传文件!');
}
}
Action
public String upload() throws Exception {
System.out.println("新导入");
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
SysUser su = (SysUser) session.getAttribute("sysUser");
int userId = su.getUserId();
System.out.println("处理文件.....");
String values = "";
String key = "";
// 连接数据库查询并返回结果
Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = null;
try {
// 清空表中数据
String delsql = "delete from " + DocumentCode.SCHEAM+ ".sys_user_xiucode where user_id='" + userId + "'";
pstmt = conn.prepareStatement(delsql);
pstmt.execute();
// 预编译SQL语句
pstmt = conn.prepareStatement("INSERT INTO " + DocumentCode.SCHEAM+ ".sys_user_xiucode" + " VALUES(?, ?)");
int cellcount = 0;
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(myfile));
StringBuffer StrB = new StringBuffer();
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
HSSFSheet sheet = workbook.getSheetAt(i);
if (null == sheet)
continue;
for (int j = 1; j <= sheet.getLastRowNum(); j++) {
HSSFRow row = sheet.getRow(j);
if (null == row)
continue;
for (int k = 0; k < row.getLastCellNum(); k++) {
HSSFCell cell = row.getCell(k);
String cellValue = "";
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String valuesub = "";
// cellValue = cell.getNumericCellValue() + "";
DecimalFormat df = new DecimalFormat("#");// 转换成整型
cellValue = df.format(cell.getNumericCellValue());
String valuerp = cellValue.replaceAll("\\.", "");
valuesub = (String) valuerp.subSequence(0, 8);
cellValue = valuesub;
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
cellValue = cell.getStringCellValue();
}
StrB.append(cellValue).append(",");
}
// 为SQL插入值
if (!StringUtil.isEmpty(cellValue)) {
// values += "'" + cellValue + "',";
pstmt.setString(1, userId + "");
pstmt.setString(2, cellValue);
pstmt.addBatch();
cellcount++;
}
}
}
}
// 批量新增
if (cellcount > 0)
pstmt.executeBatch();
key = MD5Util.EncoderByMD5_32(StrB.toString());
values = " and '" + key + "'='" + key+ "' and XIU_CODE in (select xiu_code from "+ DocumentCode.SCHEAM + ".sys_user_xiucode where user_id='"+ userId + "')";
System.out.println("readExcel2003:" + values);
log.info(userId + "导入excel:" + cellcount);
} catch (Exception e) {
e.printStackTrace();
log.error(userId + "导入excel:", e);
} finally {
DBConnection.close(null, conn, pstmt);
}
String flag = "5";
session.setAttribute("excel", values);
session.setAttribute("uploadKey", key);
return null;
}
[解决办法]
你看看 http://bbs.csdn.net/topics/370053062?page=1#post-393499108