小弟做了个客户端上传txt到服务器生成并存储到mysql数据库的方法,但是报错了,求高人。
部分debug
false
java.io.FileNotFoundException: \images\cdkey\2011\08\23\21\11082321.txt (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileReader.<init>(FileReader.java:55)
at com.pandame.service.user.impl.CDKeyCodeServiceBean.Import(CDKeyCodeServiceBean.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy63.Import(Unknown Source)
at com.pandame.web.action.user.CDKeyCodeManageAction.add(CDKeyCodeManageAction.java:196)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
主要说是系统找不到指定文件, 但是本机测试(http://127.0.0.1:8080/images/cdkey/2011/08/23/21/11082321.txt)是可以找到地址文件的。
部分代码action:
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CDKeyCodeForm formbean = (CDKeyCodeForm) form;
System.out.println(!formbean.getUploadfile().getContentType().equals("application/vnd.ms-excel"));
System.out.println(formbean.getUploadfile().getContentType().toLowerCase());
System.out.println(!formbean.getUploadfile().getContentType().toLowerCase().endsWith("text/plain"));
if(!formbean.getUploadfile().getContentType().equals("application/vnd.ms-excel")&&!formbean.getUploadfile().getContentType().toLowerCase().endsWith("text/plain")){
request.setAttribute("message", "上传文件格式不正确");
return mapping.findForward("message");
}
Date createdate = new SimpleDateFormat("yyyy-MM-dd").parse(formbean.getCreatedate());
Date expiredate = new SimpleDateFormat("yyyy-MM-dd").parse(formbean.getExpireDate());
Float price = formbean.getMoney();
String descrption = formbean.getDescrption();
ProductInfo type = productInfoService.find(formbean.getProduct());
if(formbean.getUploadfile()!=null && formbean.getUploadfile().getFileSize()>0){
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
String logopathdir = "/images/cdkey/"+ dateformat.format(new Date());//构建图片保存的目录
//得到图片保存目录的真实路径
String logorealpathdir = WebUtil.getRealPath(request,logopathdir);
File logosavedir = new File(logorealpathdir);
if(!logosavedir.exists()) logosavedir.mkdirs();//如果目录不存在就创建
String ext = formbean.getUploadfile().getFileName().substring(formbean.getUploadfile().getFileName().lastIndexOf('.'));
SimpleDateFormat datename = new SimpleDateFormat("yyMMddHH");
String imagename = datename.format(new Date()).toString()+ ext;//构建文件名称
FileOutputStream fileoutstream = new FileOutputStream(new File(logorealpathdir, imagename));
fileoutstream.write(formbean.getUploadfile().getFileData());
fileoutstream.close();
String logopath = logopathdir+"/"+imagename;
try {
//导入cdk
cdkeyService.Import(type, logopath, createdate, expiredate, price,descrption);
} catch (Exception e) {
request.setAttribute("message", "表中有错误信息");
request.setAttribute("urladdress", SiteUrl.readUrl("control.cdkey.list"));
return mapping.findForward("message");
}
}
request.setAttribute("message", "导入成功");
request.setAttribute("urladdress", SiteUrl.readUrl("control.cdkey.list"));
return mapping.findForward("message");
}
Import方法代码:
CDKeyCode cdk = null;
String filetype = filename.substring(filename.lastIndexOf(".")+1, filename.length());
if("txt".equals(filetype)){
String strs = "";
String[] s = null;
try {
FileReader read = new FileReader(new File(filename)); debug到这就直接报IO错误了
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while(d!=-1){
String str = new String(ch,0,d);
sb.append(str);
d = read.read(ch);
}
String aa = sb.toString().replaceAll("@@@@@", ",");
strs = aa.substring(0,aa.length()-1);
s = strs.split("\\r\\n");
for (int a = 0; a < s.length; a++) {
cdk = new CDKeyCode();
cdk.setCreatedate(createdate);
cdk.setExpireDate(expireDate);
cdk.setMoney(price);
cdk.setDescrption(descrption);
cdk.setProduct(typeid);
cdk.setCodeid(s[a]);
save(cdk);
}
System.out.println("导入cdk成功!");
} catch (IOException e) {
e.printStackTrace();
}
求高手解决 谢谢!
部分debug
false
java.io.FileNotFoundException: \images\cdkey\2011\08\23\21\11082321.txt (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileReader.<init>(FileReader.java:55)
at com.pandame.service.user.impl.CDKeyCodeServiceBean.Import(CDKeyCodeServiceBean.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy63.Import(Unknown Source)
at com.pandame.web.action.user.CDKeyCodeManageAction.add(CDKeyCodeManageAction.java:196)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
主要说是系统找不到指定文件, 但是本机测试(http://127.0.0.1:8080/images/cdkey/2011/08/23/21/11082321.txt)是可以找到地址文件的。
部分代码action:
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CDKeyCodeForm formbean = (CDKeyCodeForm) form;
System.out.println(!formbean.getUploadfile().getContentType().equals("application/vnd.ms-excel"));
System.out.println(formbean.getUploadfile().getContentType().toLowerCase());
System.out.println(!formbean.getUploadfile().getContentType().toLowerCase().endsWith("text/plain"));
if(!formbean.getUploadfile().getContentType().equals("application/vnd.ms-excel")&&!formbean.getUploadfile().getContentType().toLowerCase().endsWith("text/plain")){
request.setAttribute("message", "上传文件格式不正确");
return mapping.findForward("message");
}
Date createdate = new SimpleDateFormat("yyyy-MM-dd").parse(formbean.getCreatedate());
Date expiredate = new SimpleDateFormat("yyyy-MM-dd").parse(formbean.getExpireDate());
Float price = formbean.getMoney();
String descrption = formbean.getDescrption();
ProductInfo type = productInfoService.find(formbean.getProduct());
if(formbean.getUploadfile()!=null && formbean.getUploadfile().getFileSize()>0){
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
String logopathdir = "/images/cdkey/"+ dateformat.format(new Date());//构建图片保存的目录
//得到图片保存目录的真实路径
String logorealpathdir = WebUtil.getRealPath(request,logopathdir);
File logosavedir = new File(logorealpathdir);
if(!logosavedir.exists()) logosavedir.mkdirs();//如果目录不存在就创建
String ext = formbean.getUploadfile().getFileName().substring(formbean.getUploadfile().getFileName().lastIndexOf('.'));
SimpleDateFormat datename = new SimpleDateFormat("yyMMddHH");
String imagename = datename.format(new Date()).toString()+ ext;//构建文件名称
FileOutputStream fileoutstream = new FileOutputStream(new File(logorealpathdir, imagename));
fileoutstream.write(formbean.getUploadfile().getFileData());
fileoutstream.close();
String logopath = logopathdir+"/"+imagename;
try {
//导入cdk
cdkeyService.Import(type, logopath, createdate, expiredate, price,descrption);
} catch (Exception e) {
request.setAttribute("message", "表中有错误信息");
request.setAttribute("urladdress", SiteUrl.readUrl("control.cdkey.list"));
return mapping.findForward("message");
}
}
request.setAttribute("message", "导入成功");
request.setAttribute("urladdress", SiteUrl.readUrl("control.cdkey.list"));
return mapping.findForward("message");
}
Import方法代码:
CDKeyCode cdk = null;
String filetype = filename.substring(filename.lastIndexOf(".")+1, filename.length());
if("txt".equals(filetype)){
String strs = "";
String[] s = null;
try {
FileReader read = new FileReader(new File(filename)); debug到这就直接报IO错误了
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while(d!=-1){
String str = new String(ch,0,d);
sb.append(str);
d = read.read(ch);
}
String aa = sb.toString().replaceAll("@@@@@", ",");
strs = aa.substring(0,aa.length()-1);
s = strs.split("\\r\\n");
for (int a = 0; a < s.length; a++) {
cdk = new CDKeyCode();
cdk.setCreatedate(createdate);
cdk.setExpireDate(expireDate);
cdk.setMoney(price);
cdk.setDescrption(descrption);
cdk.setProduct(typeid);
cdk.setCodeid(s[a]);
save(cdk);
}
System.out.println("导入cdk成功!");
} catch (IOException e) {
e.printStackTrace();
}
求高手解决 谢谢!
String logopathdir = "/images/cdkey/"+ dateformat.format(new Date());//构建图片保存的目录
与
SimpleDateFormat datename = new SimpleDateFormat("yyMMddHH");
String imagename = datename.format(new Date()).toString()+ ext;//构建文件名称
是两个目录吧
再一个建议将"yyyy/MM/dd/HH"这个格式换成"yyyy-MM-dd-HH",前者表示目录了
依然报错 信息: Server startup in 11565 ms
true
text/plain
false
java.io.FileNotFoundException: \images\cdkey\11082321.txt (系统找不到指定的路径。)
郁闷 求解
filename其实应该传入的是已经上传到服务器的绝对路径,或者是你本地上传到服务器的File对象。
这是我watch查看的路径 没问题啊 文件名缩小 也是出错
这个是web发布的路径,是可以访问,但是你要访问服务器的文件,必须用服务器本地的路径,而不是url路径,如c:\xx.txt