1 package net.argius.stew.command;
6 import net.argius.stew.*;
9 * The Upload command used to upload a file into specified column.
11 public final class Upload extends Command {
13 private static final Logger log = Logger.getLogger(Upload.class);
16 public void execute(Connection conn, Parameter parameter) throws CommandException {
17 if (!parameter.has(2)) {
18 throw new UsageException(getUsage());
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);
26 if (file.length() > Integer.MAX_VALUE) {
27 throw new CommandException("file too large: " + file);
29 final int length = (int)file.length();
31 InputStream is = new FileInputStream(file);
33 PreparedStatement stmt = conn.prepareStatement(sql);
36 stmt.setBinaryStream(1, is, length);
37 final int updatedCount = stmt.executeUpdate();
38 outputMessage("i.updated", updatedCount);
45 } catch (IOException ex) {
46 throw new CommandException(ex);
47 } catch (SQLException ex) {
48 throw new CommandException(ex);