Ext 文件上传

[color=red]Ext.form.TextField 中设置 inputType:'file'
再将form里面的fileUpload设置为true就行了[/color]

Java代码
1.import java.io.File;
2.import java.io.FileInputStream;
3.import java.io.FileOutputStream;
4.import java.io.InputStream;
5.import java.sql.Connection;
6.import java.sql.DriverManager;
7.import java.sql.PreparedStatement;
8.import java.sql.ResultSet;
9.
10.
11.public class Dbtest {
12. private static final String URL = "jdbc:mysql://localhost:3306/jiejie?user=root&password=&useUnicode=true";
13. private Connection conn = null;
14. private PreparedStatement pstmt = null;
15. private ResultSet rs = null;
16. private File file = null;
17.
18.
19. /**
20. * 向数据库中插入一个新的BLOB对象(图片)
21. * @param infile 要输入的数据文件
22. * @throws java.lang.Exception
23. */
24. public void blobInsert(String infile) throws Exception
25. {
26. FileInputStream fis = null;
27. try
28. {
29. Class.forName("org.gjt.mm.mysql.Driver").newInstance();
30. conn = DriverManager.getConnection(URL);
31.
32. file = new File(infile);
33. fis = new FileInputStream(file);
34. pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
35. pstmt.setString(1,file.getName()); //把传过来的第一个参数设为文件名
36. pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容
37. pstmt.executeUpdate();
38. }
39. catch(Exception ex)
40. {
41. System.out.println("[blobInsert error : ]" + ex.toString());
42. }
43. finally
44. {
45. //关闭所打开的对像//
46. pstmt.close();
47. fis.close();
48. conn.close();
49. }
50. }
51.
52.
53. /**
54. * 从数据库中读出BLOB对象
55. * @param outfile 输出的数据文件
56. * @param picID 要取的图片在数据库中的ID
57. * @throws java.lang.Exception
58. */
59.
60. public void blobRead(String outfile,String picName) throws Exception
61. {
62. FileOutputStream fos = null;
63. InputStream is = null;
64. byte[] Buffer = new byte[4096];
65.
66. try
67. {
68. Class.forName("org.gjt.mm.mysql.Driver").newInstance();
69. conn = DriverManager.getConnection(URL);
70. pstmt = conn.prepareStatement("select pic from tmp where descs=?");
71. pstmt.setString(1,picName); //传入要取的图片的ID
72. rs = pstmt.executeQuery();
73. rs.next();
74.
75. file = new File(outfile);
76. if(!file.exists())
77. {
78. file.createNewFile(); //如果文件不存在,则创建
79. }
80. fos = new FileOutputStream(file);
81. is = rs.getBinaryStream("pic");
82. int size = 0;
83. while((size = is.read(Buffer)) != -1)
84. {
85. //System.out.println(size);
86. fos.write(Buffer,0,size);
87. }
88.
89. }
90. catch(Exception e)
91. {
92. System.out.println("[OutPutFile error : ]" + e.getMessage());
93. }
94. finally
95. {
96. //关闭用到的资源
97. fos.close();
98. rs.close();
99. pstmt.close();
100. conn.close();
101. }
102. }
103.
104. public static void main(String[] args)
105. {
106. try
107. {
108.
109. Dbtest blob = new Dbtest();
110. //blob.blobInsert("C:/jie1.jpg");
111. blob.blobRead("c:/1.jpg","jie.jpg");
112. }
113. catch(Exception e)
114. {
115. System.out.println("[Main func error: ]" + e.getMessage());
116. }
117. }
118.
119.}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值