ファイルをmysqlデータベースにアップロードしようとしています。<input type = "file">に対応するJavaの種類は何ですか?
<!DOCTYPE html>
<html xmlns:th="http://www.themyleaf.org">
<head>
<meta charset="ISO-8859-1" />
<title>User</title>
</head>
<body>
<h1>Please enter the following to upload the file.</h1>
<form action="#" th:action="@{/uploadToDB}" th:object="${doc}" method="post">
<p>Author: <input type="text" th:field="*{author}" /></p>
<p>Publication date: <input type="text" th:field="*{pubDate}" /></p>
<p>Title: <input type="text" th:field="*{title}" /></p>
<p>Editor: <input type="text" th:field="*{editor}" /></p>
<p>Content type: <input type="text" th:field="*{contentType}" /></p>
<p>Size: <input type="text" th:field="*{size}" /></p>
<p>Content: <input name="userfile" type="file" id="userfile" th:field="*{content}"></input></p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"></input>
<p><input type="submit" value="Upload" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
これらの値はその後、私の豆ドキュメントに設定されています:ここで
は私のフォームの@SuppressWarnings("serial")
@Component
public class Document implements Serializable {
private String author;
private String pubDate;
private String title;
private String uploadDate;
private String editor;
private String id;
private String contentType;
private String size;
private Blob content;
//setters and getters
今、私が直面しています問題は、私はによって暗黙の種類が分からないです<input type="file">
。 content
をBlob
に設定するとエラーが発生します。これは私のデータベースに挿入するタイプです。
私はこれまでにString
を使用しましたが、どちらも良いタイプではないようです。私は自分のデータベースに挿入することができます。しかし、データベースからファイルを取り出して表示したり、ダウンロードしたりすることはできません。
私は助けていただきありがとうございます!
バイト[]を試してください。内容を文字列に読み込んだら、それを使って望むことを行うことができます。 – duffymo
ありがとうございます。これを試してみよう。* – Chuck
呼び出しがうまくいけば、私は直接 'byte []'を使うことができ、 'PreparedStatement ps = connect.prepareStatement(sql); ps.setBytes(8、d.getContentFromForm()); '。私はファイルを取得しようとすると、これは私の問題を解決すると思います。ありがとう。 – Chuck