У меня есть этот код на моей стороне JSP:
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Select File : </td>
<td><input name="file" type="file"/> </td>
</tr>
<tr>
<td>Enter Filename : </td>
<td><input type="text" name="photoname" size="20"/> </td>
</tr>
</table>
<p/>
<input type="submit" value="Upload File"/>
</form>
</body>
Это загружает один файл.
И в сервлете, у меня есть код:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Part p1 = request.getPart("file");
InputStream is = p1.getInputStream();
Part p2 = request.getPart("photoname");
Scanner s = new Scanner(p2.getInputStream());
String filename = s.nextLine();
// get filename to use on the server
String outputfile = "C:\\Documents and Settings\\Sherman\\Desktop\\ImoveiSP\\ImoveiSP\\DB_Scripts" + filename + ".jpg";
//outputfile = outputfile + ".jpg";
//System.out.println("out = " + outputfile);
FileOutputStream os = new FileOutputStream(outputfile);
// write bytes taken from uploaded file to target file
int ch = is.read();
while (ch != -1) {
os.write(ch);
ch = is.read();
}
os.close();
out.println("<h3>File uploaded successfully! </h3>");
File file = new File("C:\\Documents and Settings\\Sherman\\Desktop\\ImoveiSP\\ImoveiSP\\DB_Scripts" + filename + ".jpg");
uploadAmazon(file, "ibagem", "");
} catch (Exception ex) {
out.println("Exception -->" + ex.getMessage());
} finally {
out.close();
}
}
Этот сервлет берет загруженный файл и сохраняет его на диск.
У меня есть два вопроса по поводу этих кодов:
- На стороне jsp, как я могу заставить пользователя отправитьтолько файлы .jpg или .mpg?
- Если я добавлю более одного ввода для загрузки на стороне jsp, как я получу все в сервлете?