// 上传一张照片
public ActionForward sendOnePhoto(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) { PhotoManageForm photoForm = (PhotoManageForm) form;
HttpSession session = request.getSession(); session.removeAttribute("photoname");
String ep_no = (String) session.getAttribute("ep_no"); String photo_name = photoForm.getPhoto_name();// 图片别名
if (photo_name == null || "".equals(photo_name)) {
request.setAttribute("photoname", "isEmpty");
return new ActionForward("/searchPhoto.do?flag=manage");
}
FormFile file = photoForm.getFile();
String filename = file.getFileName();// 图片的源名+扩展名
// photo_name=trans(photo_name); session.setAttribute("photoname", photo_name);
int filesize = file.getFileSize();
String firstname = ""; if (isTokenValid(request, true)) {
try {
PhotoNameSearch namesearch = new PhotoNameSearch();
firstname = namesearch.String2Alpha(photo_name).substring(0, 1);// 取图片别名首拼音
System.out.println("首拼音是:" + firstname);
} catch (Exception ex) {
ex.printStackTrace();
}
// 用作文件名的处理
Date date = new Date();
String datename = date.toString();
datename = datename.replaceAll(" ", "");
datename = datename.replaceAll(":", "");
String sql = "";// SQL语句 if (filename != null && !filename.equals("")) {
int index = filename.lastIndexOf(".");// 得到.的位置
String sn = filename.substring(index, filename.length()); // 求得扩展名 System.out.println(!sn.equalsIgnoreCase(".gif"));
System.out.println(!sn.equalsIgnoreCase(".jpg"));
System.out.println(!sn.equalsIgnoreCase(".png"));
if (!sn.equalsIgnoreCase(".gif")
&& !sn.equalsIgnoreCase(".jpg")
&& !sn.equalsIgnoreCase(".png")) {
// //跳到管理像册的上传单张照片的页面
return new ActionForward("/searchPhoto.do?selepno=" + ep_no
+ "&error=format");
}
if (filesize > 1024 * 1024 * 5) {
return new ActionForward("/searchPhoto.do?selepno=" + ep_no
+ "&error=size");
} Calendar c = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String datetime = f.format(c.getTime());
System.out.println("====" + datetime); String savepath = request.getRealPath("/") + "/upload/photo/"
+ datename + sn;// 上传文件写到相应路径目录下
sql = "insert into photo(ep_no,photo_name,photo_route,upload_date,firstname) values('"
+ StrTrans.transGbk(ep_no)
+ "','"
+ StrTrans.transGbk(photo_name)
+ "','"
+ StrTrans.transGbk(datename + sn)
+ "','"
+ datetime
+ "','" + firstname + "')";
System.out.println("the sql 1 is :" + sql); CommonDataBean commonDataBean = null;
InputStream in = null;
FileOutputStream out = null; try {
in = file.getInputStream();
out = new FileOutputStream(savepath);
commonDataBean = new CommonDataBean();
byte[] b = new byte[30000000];
int i = 0;
while ((i = in.read(b)) != -1) {
out.write(b, 0, i);
} out.close();
in.close();
commonDataBean.executeUpdate(sql);
// 更新上传时间
photoHandle ph = new photoHandle();
ph.updatetime(ep_no);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("something wrong!");
} finally {
commonDataBean.closeConn();
}
}
resetToken(request);
} else {
saveToken(request);
}
return new ActionForward("/searchPhoto.do?flag=manage");
}没有组织好代码,呵呵!这是有问题的代码,但是不知道问题在哪,
错误提示是http 500 dispatch[/photoManage] ...return...exception 具体的就这了,这应该是ActionForward问题,但是我换了个再其他页面能跳转的forward还是出这个问题,而且关键是用这个它有时上传成功,有时不成功郁闷

解决方案 »

  1.   

    我有个疑问,struts的token会不会影响代码?
      

  2.   

    但是token只是防重复提交的,与代码的逻辑关系不大,所以我比较矛盾啊
      

  3.   

    我觉得你有可能是文件重名了!
    你最好可以单步跟踪一下,现在我们不是很清楚你的Exception到底是什么。
    你把那段错误提示整个copy下来看看,ok?
      

  4.   

    报的是return异常,,但是为什么有时候上传成功,有时候不成功呢?