imageUp.jsp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ page import="java.util.Properties" %>
  4. <%@ page import="java.util.List" %>
  5. <%@ page import="java.util.Iterator" %>
  6. <%@ page import="java.util.Arrays" %>
  7. <%@ page import="java.io.FileInputStream" %>
  8. <%@ page import="ueditor.Uploader" %>
  9. <%@ page import="java.io.File" %>
  10. <%@ page import="java.util.Map" %>
  11. <%@ page import="com.zhcs.dt.util.*" %>
  12. <%
  13. request.setCharacterEncoding( Uploader.ENCODEING );
  14. response.setCharacterEncoding( Uploader.ENCODEING );
  15. String currentPath = request.getRequestURI().replace( request.getContextPath(), "" );
  16. File currentFile = new File( currentPath );
  17. currentPath = currentFile.getParent() + File.separator;
  18. //System.out.println("=================="+currentPath);
  19. //加载配置文件
  20. //String propertiesPath = request.getSession().getServletContext().getRealPath( currentPath + "config.properties" );
  21. String propertiesPath = request.getRealPath("/plugins/ueditor/jsp/config.properties");
  22. Properties properties = new Properties();
  23. try {
  24. properties.load( new FileInputStream( propertiesPath ) );
  25. } catch ( Exception e ) {
  26. //加载失败的处理
  27. e.printStackTrace();
  28. }
  29. List<String> savePath = Arrays.asList( properties.getProperty( "savePath" ).split( "," ) );
  30. //获取存储目录结构
  31. if ( request.getParameter( "fetch" ) != null ) {
  32. response.setHeader( "Content-Type", "text/javascript" );
  33. //构造json数据
  34. Iterator<String> iterator = savePath.iterator();
  35. String dirs = "[";
  36. while ( iterator.hasNext() ) {
  37. dirs += "'" + iterator.next() +"'";
  38. if ( iterator.hasNext() ) {
  39. dirs += ",";
  40. }
  41. }
  42. dirs += "]";
  43. response.getWriter().print( "updateSavePath( "+ dirs +" );" );
  44. return;
  45. }
  46. Uploader up = new Uploader(request);
  47. // 获取前端提交的path路径
  48. String dir = request.getParameter( "dir" );
  49. //普通请求中拿不到参数, 则从上传表单中拿
  50. if ( dir == null ) {
  51. dir = up.getParameter("dir");
  52. }
  53. if ( dir == null || "".equals( dir ) ) {
  54. //赋予默认值
  55. dir = savePath.get( 0 );
  56. //安全验证
  57. } else if ( !savePath.contains( dir ) ) {
  58. response.getWriter().print( "{'state':'\\u975e\\u6cd5\\u4e0a\\u4f20\\u76ee\\u5f55'}" );
  59. return;
  60. }
  61. up.setSavePath( dir );
  62. String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
  63. up.setAllowFiles(fileType);
  64. up.setMaxSize(500 * 1024); //单位KB
  65. up.upload();
  66. response.getWriter().print("{'original':'"+up.getOriginalName()+"','url':'"+up.getUrl()+"','title':'"+up.getTitle()+"','state':'"+up.getState()+"'}");
  67. %>