码迷,mamicode.com
首页 > 编程语言 > 详细

使用jspSmartUpload.jar上传图片的路径问题

时间:2015-05-07 10:21:06      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

     程序用eclipse编写,首先把jspsmartupload.jar包放到WEB-INF/lib下,然后在jsp页面import; 

   <!-- SmartUpload组件用到的文件上传类 -->
   <%@ page import="com.jspsmart.upload.*"%>
   <%@ page import="com.jspsmart.upload.File"%>

   上传过程:

  

<%
SmartUpload mySmartUpload =new SmartUpload();

long file_size_max=4000000;

String fileName2="",ext="",testvar="";

 

//初始化

mySmartUpload.initialize(pageContext);

//只允许上载此类文件

try {

mySmartUpload.setAllowedFilesList("jpg,gif");//此处的文件格式可以根据需要自己修改

//上载文件

mySmartUpload.upload();

} catch (Exception e){

%>

<script type="text/javascript">

alert("只允许上传.jpg和.gif类型图片文件");

window.location=‘webdesign.jsp‘;

</script>

<%

}

try{

com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);

if (myFile.isMissing()){%>

<script type="text/javascript">

alert("请先选择要上传的文件");

window.location=‘webdesign.jsp‘;

</script>

<%}

else{

//String myFileName=myFile.getFileName(); //取得上载的文件的文件名

ext= myFile.getFileExt(); //取得后缀名

int file_size=myFile.getSize(); //取得文件的大小

String saveurl="";

if(file_size<file_size_max){

//更改文件名,取得当前上传时间的毫秒数值

Calendar calendar = Calendar.getInstance();

saveurl="/upload/" + myFile.getFileName(); //应保证tomcat所在根目录中有此upload目录的存在(也就是说需要自己建立相应的文件夹)

myFile.saveAs(saveurl);
out.print(saveurl);

}

}

}catch (Exception e){


out.print(e.toString());

}
%>

 红色部分就是容易导致路径报错:

java.lang.IllegalArgumentException: This path does not exist (1135)

  解决办法就是:保证tomcat所在磁盘根目录中有此upload目录的存在(也就是说需要自己建立相应的文件夹)

  (其实写个函数判断路径是否存在,如不存在就自动创建目录应该也是可以解决的,不过还没来得及尝试)

 

使用jspSmartUpload.jar上传图片的路径问题

标签:

原文地址:http://www.cnblogs.com/seaperson/p/4483977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!