struts2:配置<constant name="struts.multipart.saveDir" value="d:\tmp"/>
<constant name="struts.multipart.maxSize" value="100000000"/>
JSP:代码:<input type="file" id="upFile" name="upFile">
<input type="button" value="上传" onclick="uploadFile()"/>
<input id="imgPath" type="hidden" name="photoPath"/>
JS代码:function uploadFile(){
//调用插件的上传方法
if($("#upFile").val()==""){
return false;
}else{
$.ajaxFileUpload({
url:"uploadImg.action",
fileElementId:"upFile",
secureuri:false,
dataType:"json",
success:function(data,status){
if(status == "success"){
}else{
}
}
});
}
}
action代码:private File upFile; //文件
private String upFileFileName; //文件名
private String upFileContentType;
private String filePath; //文件路径
get set..
public void uploadImg(){
String path = ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(path); // 判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists()) {
file.mkdir();
}
try {
if (this.upFile != null) {
File f = this.getUpFile();
String fileName = java.util.UUID.randomUUID().toString(); // 采用时间+UUID的方式随即命名
System.out.println(upFileFileName);
String name = fileName+ upFileFileName.substring(upFileFileName.lastIndexOf(".")); // 保存在硬盘中的文件名
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path+ "\\" + name);
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
//文件保存的完整路径 比如:D:\tomcat6\webapps\eserver\\upload\a0be14a1-f99e-4239-b54c-b37c3083134a.png
filePath = path+"\\"+name;
}
} catch (Exception e) {
e.printStackTrace();
}
}
错误;上传不成功。输出这句话[ INFO] (CommonsLogger.java:31) - Removing file upFile d:\tmp\upload__595ab118_14b050e3e0f__8000_00000000.tmp,搞不清楚啥原因啊来个大神指点下吧.
------解决思路----------------------
貌似是临时删除文件有问题么? 我来学习学习