在站内搜索了一下,没有找到类似的问题
1。页面只有上传的的form,包括:file框,text框,上传按钮。有时候能上传,有时不能上传成功,不成功提示http 500 dispatch[/photoManage]...错误,但是同样一个ActionForward为什么有时可以,有时不可以。
2。struts自带的token会不会影响上传呢?
3。是不是代码有问题,各位同胞们帮我看下,代码如下:
java 代码
- // 上传一张照片
- //class photoManage
- 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();// 图片的源名+扩展名
- 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()); // 求得扩展名
- 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 + "')";
- 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");
- }