This question already has an answer here:
这个问题在这里已有答案:
- What does a “Cannot find symbol” compilation error mean? 10 answers
“无法找到符号”编译错误是什么意思? 10个答案
Part part = request.getPart("file");
if (part != null){
String fileName = extractFileName(part);
String filePath = savePath + File.separator + fileName;
part.write(savePath + File.separator + fileName);
String imageName = fileName;
} else{
String fileName = "avatar.jpg";
String filePath = savePath + File.separator + fileName;
part.write(savePath + File.separator + fileName);
String imageName = fileName;
}
After inserting the if else statement into the code, my code at the bottom gotten this error saying: imageName cannot be resolved to a variable and filePath cannot be resolved to a variabl. However, once i comment away my if else statement everything is fine. Can someone tell me where is the error?
在将if else语句插入代码后,我底部的代码得到了这样的错误:imageName无法解析为变量,filePath无法解析为变量。然而,一旦我评论了我的if else声明一切都很好。有人能告诉我哪里出错了吗?
request.setAttribute("Pic", filePath);
request.setAttribute("PicName", imageName);
2 个解决方案
#1
5
Your "filePath" and "imageName" variables are only visible from within the if or else blocks. Declare these variables before the if/then blocks and then set the variable in the if/then code rather than re-declaring it.
您的“filePath”和“imageName”变量仅在if或else块中可见。在if / then块之前声明这些变量,然后在if / then代码中设置变量,而不是重新声明它。
String filePath = "";
String imageName = "";
if (...) {
...
} else {
...
}
request.setAttribute("Pic", filePath);
request.setAttribute("PicName", imageName);
See http://www.java-made-easy.com/variable-scope.html for more information on scopes.
有关范围的更多信息,请参见http://www.java-made-easy.com/variable-scope.html。
#2
1
Agreed with @CConrad96. However, there are even more improvements that could be done, for example, the last 3 lines on the if
and else
are the same. Also, if you will write imageName = fileName
why not get rid of one ? Last, the argument on part.write
and has the same value as filePath
, why not use it ?
同意@ CConrad96。但是,还有更多的改进可以做,例如,if和else上的最后3行是相同的。另外,如果你要写imageName = fileName为什么不去掉一个?最后,part.write上的参数和filePath具有相同的值,为什么不使用它呢?
String fileName:
Part part = request.getPart("file");
if (part != null){
fileName = extractFileName(part);
} else{
fileName = "avatar.jpg";
}
String filePath = savePath + File.separator + fileName;
part.write(filePath);
request.setAttribute("Pic", filePath);
request.setAttribute("PicName", fileName);
#1
5
Your "filePath" and "imageName" variables are only visible from within the if or else blocks. Declare these variables before the if/then blocks and then set the variable in the if/then code rather than re-declaring it.
您的“filePath”和“imageName”变量仅在if或else块中可见。在if / then块之前声明这些变量,然后在if / then代码中设置变量,而不是重新声明它。
String filePath = "";
String imageName = "";
if (...) {
...
} else {
...
}
request.setAttribute("Pic", filePath);
request.setAttribute("PicName", imageName);
See http://www.java-made-easy.com/variable-scope.html for more information on scopes.
有关范围的更多信息,请参见http://www.java-made-easy.com/variable-scope.html。
#2
1
Agreed with @CConrad96. However, there are even more improvements that could be done, for example, the last 3 lines on the if
and else
are the same. Also, if you will write imageName = fileName
why not get rid of one ? Last, the argument on part.write
and has the same value as filePath
, why not use it ?
同意@ CConrad96。但是,还有更多的改进可以做,例如,if和else上的最后3行是相同的。另外,如果你要写imageName = fileName为什么不去掉一个?最后,part.write上的参数和filePath具有相同的值,为什么不使用它呢?
String fileName:
Part part = request.getPart("file");
if (part != null){
fileName = extractFileName(part);
} else{
fileName = "avatar.jpg";
}
String filePath = savePath + File.separator + fileName;
part.write(filePath);
request.setAttribute("Pic", filePath);
request.setAttribute("PicName", fileName);