upload_re.jsp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <%@ page contentType="text/html;charset=utf-8" language="java" import="com.jspsmart.upload.*"%>
  2. <%@ page import="com.jspsmart.upload.*"%>
  3. <%@ page import="java.util.*"%>
  4. <%
  5. String path = request.getContextPath();
  6. %>
  7. <%
  8. String newFile1Name=null;
  9. String file_name=null;
  10. SmartUpload mySmartUpload = new SmartUpload();
  11. //初始化上传
  12. mySmartUpload.initialize(pageContext);
  13. //只允许上载此类文件
  14. try
  15. {
  16. //mySmartUpload.setAllowedFilesList("jpg,Jpg,JPG,GIF,gif,Gif,png");
  17. mySmartUpload.upload();
  18. }
  19. catch (Exception e)
  20. {
  21. //out.println("<script language=javascript>alert('上传格式错误!'); history.back(-1);</script>");
  22. //return;
  23. }
  24. try
  25. {
  26. com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
  27. if (myFile.isMissing())
  28. {
  29. out.println("<script language=javascript>alert('必须选择图片!'); history.back(-1);</script>");
  30. return;
  31. }
  32. else
  33. {
  34. int file_size = myFile.getSize(); //取得文件的大小 (单位是b)
  35. file_name=myFile.getFileName();
  36. System.out.println("文件大小:"+file_size+"文件名称:"+file_name);
  37. //if (file_size > 10*1024*1024)
  38. //{
  39. //out.println("<script language=javascript>alert('上传图片大小应控制在10K~1M之间!'); history.back(-1);</script>");
  40. //return;
  41. //}
  42. //else
  43. //{
  44. newFile1Name=new Date().getTime()+file_name.substring(file_name.indexOf("."));
  45. System.out.println("新文件名称:"+newFile1Name);
  46. String saveurl = request.getSession().getServletContext().getRealPath("upload");
  47. saveurl = saveurl+"/"+newFile1Name;
  48. myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);
  49. // }
  50. }
  51. }
  52. catch (Exception e)
  53. {
  54. e.toString();
  55. }
  56. %>
  57. <script language="javascript">
  58. document.write("上传成功");
  59. window.parent.document.getElementById("fujian").value="/upload/<%= newFile1Name%>";
  60. window.parent.document.getElementById("fujianYuanshiming").value="<%= file_name%>";
  61. </script>