OSDN Git Service

new repository
[stew/Stew4.git] / src / net / argius / stew / command / Upload.java
1 package net.argius.stew.command;
2
3 import java.io.*;
4 import java.sql.*;
5
6 import net.argius.stew.*;
7
8 /**
9  * The Upload command used to upload a file into specified column.
10  */
11 public final class Upload extends Command {
12
13     private static final Logger log = Logger.getLogger(Upload.class);
14
15     @Override
16     public void execute(Connection conn, Parameter parameter) throws CommandException {
17         if (!parameter.has(2)) {
18             throw new UsageException(getUsage());
19         }
20         final File file = resolvePath(parameter.at(1));
21         final String sql = parameter.after(2);
22         if (log.isDebugEnabled()) {
23             log.debug("file: " + file.getAbsolutePath());
24             log.debug("SQL: " + sql);
25         }
26         if (file.length() > Integer.MAX_VALUE) {
27             throw new CommandException("file too large: " + file);
28         }
29         final int length = (int)file.length();
30         try {
31             InputStream is = new FileInputStream(file);
32             try {
33                 PreparedStatement stmt = conn.prepareStatement(sql);
34                 try {
35                     setTimeout(stmt);
36                     stmt.setBinaryStream(1, is, length);
37                     final int updatedCount = stmt.executeUpdate();
38                     outputMessage("i.updated", updatedCount);
39                 } finally {
40                     stmt.close();
41                 }
42             } finally {
43                 is.close();
44             }
45         } catch (IOException ex) {
46             throw new CommandException(ex);
47         } catch (SQLException ex) {
48             throw new CommandException(ex);
49         }
50     }
51
52 }