url==/WebFriend/userimg/1307029058093.jpgjava.lang.IllegalArgumentException: This path does not exist (1135).
这是代码..就是在file.saveAs这边会出错,我想不通了我工程路径是对的,也有这个文件夹..为什么它老是报错
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%@page import="com.jspsmart.upload.*" %>
<%@page import="java.awt.*" %>
<%@page import="java.awt.image.*" %>
<%@page import="com.sun.image.codec.jpeg.*" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<%
try
{
String url= "jdbc:mysql://localhost:3306/message";
String user="root";
String password="root"; SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setMaxFileSize(2000000);
su.setTotalMaxFileSize(20000000);
su.setAllowedFilesList("jpg,gif,bmp,png");
su.upload();
for (int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if (file.isMissing()) continue;
java.util.Date d =new java.util.Date();
Long l = d.getTime();
System.out.print("url=="+request.getContextPath()+"/userimg/" +l.toString()+ "."+file.getFileExt());
file.saveAs(request.getContextPath()+"/userimg/" +l.toString()+ "."+file.getFileExt());
java.io.File yuan = new java.io.File(request.getContextPath()+"/userimg/" +"\\"+l.toString()+ "."+file.getFileExt()); //读入刚才上传的文件
System.out.print(yuan);
String newurl=request.getContextPath()+"/userimg/" +"\\"+l.toString()+ "_s."+file.getFileExt(); //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(yuan); //构造Image对象
int new_w=150;
int new_h=150;
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
out.print("id======="+Integer.parseInt(session.getAttribute("id").toString()));
String opath=request.getContextPath()+"/userimg/"+l.toString()+ "."+file.getFileExt();
String spath=request.getContextPath()+"/userimg/"+l.toString()+ "_s."+file.getFileExt(); request.setCharacterEncoding("gb2312");
Class.forName("com.mysql.jdbc.Driver"); //载入JDBC驱动程序
Connection conn=DriverManager.getConnection(url,user,password);; //创建数据库连接对象
PreparedStatement stmt=conn.prepareStatement("update user_table set userImg =? where id="+Integer.parseInt(session.getAttribute("id").toString()));
//预编译的 SQL 语句执行对象,参数是具体的SQL语句,未知内容用?代替
stmt.setString(1,spath);
stmt.execute(); //执行该SQL语句
stmt.close();
conn.close();
session.setAttribute("userloginimg","userimg/"+l.toString()+ "_s."+file.getFileExt());
response.sendRedirect("index_frist.jsp");
}
}
catch(Exception e)
{
out.print("文件格式错误或则太大");
System.out.print(e);
}
%>
</body>
</html>
------------------------------------------
但是只要把 file.saveAs(request.getContextPath()+"/userimg/" +l.toString()+ "."+file.getFileExt());改成
file.saveAs("/userimg/" +l.toString()+ "."+file.getFileExt());
就可以了,但是图片却保存到了MyEclipse的自带tomcat里面去了.metadata\.me_tcat\webapps\WebFriend\userimg...弄了半天没搞清楚是神马状况
求助高手
这是代码..就是在file.saveAs这边会出错,我想不通了我工程路径是对的,也有这个文件夹..为什么它老是报错
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%@page import="com.jspsmart.upload.*" %>
<%@page import="java.awt.*" %>
<%@page import="java.awt.image.*" %>
<%@page import="com.sun.image.codec.jpeg.*" %> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body>
<%
try
{
String url= "jdbc:mysql://localhost:3306/message";
String user="root";
String password="root"; SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setMaxFileSize(2000000);
su.setTotalMaxFileSize(20000000);
su.setAllowedFilesList("jpg,gif,bmp,png");
su.upload();
for (int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if (file.isMissing()) continue;
java.util.Date d =new java.util.Date();
Long l = d.getTime();
System.out.print("url=="+request.getContextPath()+"/userimg/" +l.toString()+ "."+file.getFileExt());
file.saveAs(request.getContextPath()+"/userimg/" +l.toString()+ "."+file.getFileExt());
java.io.File yuan = new java.io.File(request.getContextPath()+"/userimg/" +"\\"+l.toString()+ "."+file.getFileExt()); //读入刚才上传的文件
System.out.print(yuan);
String newurl=request.getContextPath()+"/userimg/" +"\\"+l.toString()+ "_s."+file.getFileExt(); //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(yuan); //构造Image对象
int new_w=150;
int new_h=150;
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
out.print("id======="+Integer.parseInt(session.getAttribute("id").toString()));
String opath=request.getContextPath()+"/userimg/"+l.toString()+ "."+file.getFileExt();
String spath=request.getContextPath()+"/userimg/"+l.toString()+ "_s."+file.getFileExt(); request.setCharacterEncoding("gb2312");
Class.forName("com.mysql.jdbc.Driver"); //载入JDBC驱动程序
Connection conn=DriverManager.getConnection(url,user,password);; //创建数据库连接对象
PreparedStatement stmt=conn.prepareStatement("update user_table set userImg =? where id="+Integer.parseInt(session.getAttribute("id").toString()));
//预编译的 SQL 语句执行对象,参数是具体的SQL语句,未知内容用?代替
stmt.setString(1,spath);
stmt.execute(); //执行该SQL语句
stmt.close();
conn.close();
session.setAttribute("userloginimg","userimg/"+l.toString()+ "_s."+file.getFileExt());
response.sendRedirect("index_frist.jsp");
}
}
catch(Exception e)
{
out.print("文件格式错误或则太大");
System.out.print(e);
}
%>
</body>
</html>
------------------------------------------
但是只要把 file.saveAs(request.getContextPath()+"/userimg/" +l.toString()+ "."+file.getFileExt());改成
file.saveAs("/userimg/" +l.toString()+ "."+file.getFileExt());
就可以了,但是图片却保存到了MyEclipse的自带tomcat里面去了.metadata\.me_tcat\webapps\WebFriend\userimg...弄了半天没搞清楚是神马状况
求助高手
解决方案 »
- forEach 同时遍历两个集合求解
- ECLIPSE语法着色的问题
- 请问各位大大,为什么我部署的一个项目经常莫名的死掉?
- 大家看看这个错误是怎么回事!!!
- webwork 的subset标签问题,求教各位高手
- 做一个基于j2ee架构的中型网站要多少钱?
- 关于bo和vo的疑惑
- 再累,再难,再忙也挺! 因为有我们java 家庭
- 有关tomcat服务器conf目录下的servlet.xml文件的问题?
- 在java中,有没有象Delphi中的类引用使用方法?
- 我是一名java程序员,主要开发javaee程序,想问下有没有必要学习linux?
- SSH2架构中 怎么防止SQL注入呢?还有其他相关安全问题怎么设计呢?
saveAs的时候和读取的时候用了不同的目录路径(多了个"\\")
然后,不要用File来开文件,直接用ServletContext.getResourceAsStream()方法来获取输入流好了。
如果一定要用File,先getRealPath
然后ServletContext.getResourceAsStream()开文件的时候,也不要写request.getContextPath(),保持和saveAs时使用的参数一致就行了。最后,SmartUpload现在连网站都找不到了,建议不要用了。