test.jsp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <%@ page contentType="text/html;charset=utf-8"%>
  2. <%@ page language="java" import="java.io.*,java.sql.*,java.util.*"%>
  3. <%@ page language="java"
  4. import="org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*"%>
  5. <%@ include file="connectionInfo.jsp"%>
  6. <%@ include file="FilePathInfo.jsp"%>
  7. <%!/*------------------------------------------------------------
  8. officeFileItem:文档FileItem类
  9. attachFileItem:附件的FileItem类
  10. officefileNameDisk:文档保存到磁盘上的路径
  11. attachFileNameDisk:附件保存到磁盘上的路径
  12. --------------------------------------------------------------*/
  13. public Connection conn = null;
  14. public Statement stmt = null;
  15. public PreparedStatement pstmt = null;
  16. public ResultSet rs = null;
  17. public FileItem officeFileItem = null;
  18. public FileItem attachFileItem = null;
  19. public String officefileNameDisk = "";
  20. public String attachFileNameDisk = "";
  21. /*------------------------------------------------------------
  22. 在新建文档的时候调用,返回当前文档在数据库中应该对应的id号。
  23. --------------------------------------------------------------*/
  24. public int GetMaxID() {
  25. int mResult = 0;
  26. String mSql = new String();
  27. mSql = "select max(id)+1 as MaxID from " + officeFileInfoTableName;
  28. try {
  29. rs = stmt.executeQuery(mSql);
  30. if (rs.next()) {
  31. mResult = rs.getInt("MaxID");
  32. }
  33. rs.close();
  34. if (mResult == 0)
  35. mResult = 1;
  36. mSql = "insert into " + officeFileInfoTableName + " (id) values ("
  37. + mResult + ")";
  38. stmt.execute(mSql);
  39. } catch (SQLException e) {
  40. System.out.println("error:" + e.getMessage());
  41. mResult = 0;
  42. }
  43. return (mResult);
  44. }
  45. /*------------------------------------------------------------
  46. 保存文档到服务器磁盘,返回值true,保存成功,返回值为false时,保存失败。
  47. --------------------------------------------------------------*/
  48. public boolean saveFileToDisk() {
  49. File officeFileUpload = null;
  50. File attachFileUpload = null;
  51. String officeFileUploadPath = "";
  52. String attachFileUploadPath = "";
  53. boolean result = true;
  54. try {
  55. System.out.println("officefilepath:" + absoluteOfficeFileDir
  56. + officefileNameDisk);
  57. System.out.println("attachfilepath:" + absoluteAttachFileDir
  58. + attachFileNameDisk);
  59. if (!officefileNameDisk.equalsIgnoreCase("")
  60. && officeFileItem != null) {
  61. officeFileUpload = new File(absoluteOfficeFileDir
  62. + officefileNameDisk);
  63. officeFileItem.write(officeFileUpload);
  64. }
  65. if (!attachFileNameDisk.equalsIgnoreCase("")
  66. && attachFileItem != null) {
  67. attachFileUpload = new File(absoluteAttachFileDir
  68. + attachFileNameDisk);
  69. attachFileItem.write(attachFileUpload);
  70. }
  71. } catch (FileNotFoundException e) {
  72. } catch (Exception e) {
  73. System.out.println("error saveFileToDisk:" + e.getMessage());
  74. e.printStackTrace();
  75. result = false;
  76. }
  77. return result;
  78. }
  79. /*------------------------------------------------------------
  80. 删除服务器上原有的文件,返回值true,删除成功,false,删除失败。
  81. --------------------------------------------------------------*/
  82. public boolean deleteDiskFile(int deleteFileId) {
  83. String sqlStr = "select * from " + officeFileInfoTableName
  84. + " where id=" + deleteFileId;
  85. String officeFileNameDel = "";
  86. String attachFileNameDel = "";
  87. File officeFileDel = null;
  88. File attachFileDel = null;
  89. boolean result = true;
  90. try {
  91. rs = stmt.executeQuery(sqlStr);
  92. if (rs.next()) {
  93. officeFileNameDel = rs.getString("FILENAMEDISK") == null ? ""
  94. : rs.getString("FILENAMEDISK").trim();
  95. attachFileNameDel = rs.getString("ATTACHFILENAMEDISK") == null ? ""
  96. : rs.getString("ATTACHFILENAMEDISK").trim();
  97. if (!officeFileNameDel.equalsIgnoreCase("")) {
  98. officeFileDel = new File(absoluteOfficeFileDir
  99. + officeFileNameDel);
  100. if (officeFileDel.exists()) {
  101. System.out.println("officefilefound!officefilefound!");
  102. if (!officeFileDel.delete()) {
  103. result = false;
  104. }
  105. }
  106. }
  107. if (!attachFileNameDel.equalsIgnoreCase("")
  108. && attachFileItem != null) {
  109. attachFileDel = new File(absoluteAttachFileDir
  110. + attachFileNameDel);
  111. if (attachFileDel.exists()) {
  112. System.out.println("attachfilefound!attachfilefound!");
  113. if (!attachFileDel.delete()) {
  114. result = false;
  115. }
  116. }
  117. }
  118. }
  119. rs.close();
  120. } catch (Exception e) {
  121. System.out.println("zl error:" + e.getMessage());
  122. result = false;
  123. }
  124. return result;
  125. }%>
  126. <%
  127. int fileId = 0;
  128. long fileSize = 0;
  129. String fileName = "";
  130. String otherData = "";
  131. String attachFileDescribe = "";
  132. String attachFileName = "";
  133. String fileType = "";
  134. String mySqlStr = "";
  135. String result = "";
  136. attachFileNameDisk = "";
  137. boolean isNewRecode = true;
  138. officeFileItem = null;
  139. attachFileItem = null;
  140. DiskFileItemFactory factory = new DiskFileItemFactory();
  141. // 设置最多只允许在内存中存储的数据,单位:字节
  142. factory.setSizeThreshold(4096);
  143. // 设置一旦文件大小超过setSizeThreshold()的值时数据存放在硬盘的目录
  144. factory.setRepository(new File(tempFileDir));
  145. ServletFileUpload upload = new ServletFileUpload(factory);
  146. //设置允许用户上传文件大小,单位:字节
  147. upload.setSizeMax(1024 * 1024 * 4);
  148. List fileItems = null;
  149. try {
  150. fileItems = upload.parseRequest(request);
  151. } catch (FileUploadException e) {
  152. out.println("the max upload size is 4m,cheeck upload file size!");
  153. out.println(e.getMessage());
  154. e.printStackTrace();
  155. return;
  156. }
  157. Iterator iter = fileItems.iterator();
  158. attachFileItem = null;
  159. while (iter.hasNext()) {
  160. FileItem item = (FileItem) iter.next();
  161. //打印提交的文本域和文件域名称
  162. //out.println(item.getFieldName());
  163. if (item.isFormField()) {
  164. if (item.getFieldName().equalsIgnoreCase("fileId")) {
  165. //if fileId=0,the file is new recode
  166. try {
  167. fileId = Integer.parseInt(item.getString().trim());
  168. } catch (NumberFormatException e) {
  169. System.out.println("NumberFormatException:"
  170. + e.getMessage());
  171. fileId = 0;
  172. }
  173. }
  174. if (item.getFieldName().equalsIgnoreCase("fileName")) {
  175. fileName = item.getString("utf-8").trim();
  176. }
  177. if (item.getFieldName().equalsIgnoreCase("otherData")) {
  178. otherData = item.getString("utf-8").trim();
  179. otherData = otherData.equalsIgnoreCase("") ? "请输入附加数据"
  180. : otherData;
  181. }
  182. if (item.getFieldName().equalsIgnoreCase(
  183. "attachFileDescribe")) {
  184. attachFileDescribe = item.getString("utf-8").trim();
  185. attachFileDescribe = attachFileDescribe
  186. .equalsIgnoreCase("") ? "请输入附件描述"
  187. : attachFileDescribe;
  188. }
  189. if (item.getFieldName().equalsIgnoreCase("fileType")) {
  190. fileType = item.getString("utf-8").trim();
  191. }
  192. } else {
  193. if (item.getFieldName().equalsIgnoreCase("attachFile")) {
  194. attachFileItem = item;
  195. }
  196. if (item.getFieldName().equalsIgnoreCase("upLoadFile")) {
  197. officeFileItem = item;
  198. }
  199. }
  200. }
  201. try {
  202. Class.forName(DBDriver);
  203. } catch (ClassNotFoundException e) {
  204. out.println("error" + e.getMessage());
  205. return;
  206. }
  207. try {
  208. conn = DriverManager.getConnection(ConnStr, userName,
  209. userPasswd);
  210. stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  211. ResultSet.CONCUR_UPDATABLE);
  212. } catch (SQLException e) {
  213. out.println("error:" + e.getMessage());
  214. return;
  215. }
  216. if (fileId == 0) {
  217. fileId = GetMaxID();
  218. }
  219. if (fileId != 0 && !fileName.equalsIgnoreCase("")
  220. && officeFileItem != null) {
  221. if (deleteDiskFile(fileId))//删除数据库中fileid对应的服务器文件
  222. {
  223. if (attachFileItem != null)//如果上传附件为空,应该使用数据库中原有的附件数据
  224. {
  225. attachFileNameDisk = attachFileItem.getName();
  226. attachFileNameDisk = attachFileNameDisk
  227. .substring(attachFileNameDisk.lastIndexOf("\\") + 1);
  228. //保存到磁盘中的附件为了防止重复,名字取为 fileId+".attacfile."+attachFileName
  229. attachFileNameDisk = fileId + ".attacfile."
  230. + attachFileNameDisk;
  231. } else {
  232. rs = stmt.executeQuery("select * from "
  233. + officeFileInfoTableName + " where id="
  234. + fileId);
  235. rs.next();
  236. attachFileNameDisk = rs.getString("ATTACHFILENAMEDISK") == null ? ""
  237. : rs.getString("ATTACHFILENAMEDISK").toString();
  238. rs.close();
  239. }
  240. mySqlStr = "update "
  241. + officeFileInfoTableName
  242. + " set filename=?,filesize=?,otherdata=?,filetype=?,filenamedisk=?,attachfilenamedisk=?,attachfiledescribe=? where id="
  243. + fileId;
  244. fileSize = officeFileItem.getSize();
  245. //保存到磁盘中的文档为了防止重复,名字取为 fileId+".officefile."+attachFileName
  246. officefileNameDisk = fileId + ".officefile." + fileName;
  247. //out.println(officefileUrl+"<br>"+attachFileUrl+"<br>");
  248. if (saveFileToDisk()) {
  249. try {
  250. pstmt = conn.prepareStatement(mySqlStr);
  251. pstmt.setString(1, fileName);
  252. pstmt.setInt(2, (int) fileSize);
  253. pstmt.setString(3, otherData);
  254. pstmt.setString(4, fileType);
  255. pstmt.setString(5, officefileNameDisk);
  256. pstmt.setString(6, attachFileNameDisk);
  257. pstmt.setString(7, attachFileDescribe);
  258. pstmt.execute();
  259. result = "文档保存成功。";
  260. } catch (SQLException e) {
  261. result = "SQLException,updatedatabase:"
  262. + e.getMessage();
  263. }
  264. } else {
  265. result = "save file failed,please check upload file size,the max size is 4M";
  266. }
  267. } else {
  268. result = "delete file failed";
  269. }
  270. } else {
  271. result = "wrong information";
  272. }
  273. out.println(result);
  274. try {
  275. if (pstmt != null)
  276. pstmt.close();
  277. if (stmt != null)
  278. stmt.close();
  279. if (conn != null)
  280. conn.close();
  281. } catch (Exception Error) {
  282. out.println(result + "<br>");
  283. out.println("数据库资源清理失败.");
  284. }
  285. /* conn = DriverManager.getConnection(ConnStr,userName,userPasswd);
  286. stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  287. rs = stmt.executeQuery("select * from "+officeFileInfoTableName+" fileid="+fileId);
  288. */
  289. %>