私は以下のコードを使用してファイルをサーバーにアップロードしています。それは適切ですか?コード内のパス "d:/ new"は、サーバーパスまたはクライアントPCパスです。どこでもクライアントのPCパスを指定する必要がありますか。または、参照ボタンをクリックしてファイルを選択している間、直接パスを取得しますか?ファイルをサーバーにアップロードする以外の方法がありますか?サーバーにファイル(Excel)をアップロードする
index.htmlを
<html>
<body>
<form class="form-group" action="go" method="post" enctype="multipart/form-data">
<%
Connection connection = null;
Statement statement = null;
String error = "";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lms_data", "root", "");
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery("select * from assessment_type");
connection.setAutoCommit(false);
%>
<label>Assessment Type</label>
<select class="form-control" name="assessment_type_id">
<% while (resultset.next()) {%>
<option value="<%= resultset.getString(1)%>"><%= resultset.getString(2)%></option>
<% } %>
</select>
<%
ResultSet resultset_cat_id = statement.executeQuery("select * from course_category");
%>
<br/><label>Category Name</label>
<select class="form-control" name="category_id">
<% while (resultset_cat_id.next()) {%>
<option value="<%= resultset_cat_id.getString(1)%>"><%= resultset_cat_id.getString(2)%></option>
<% } %>
</select>
<%
} catch (Exception e) {
if (!connection.isClosed()) {
connection.close();
}
out.println(e);
e.printStackTrace();
}
%>
<label class="control-label">Select File</label>
<input type="file" name="file" id="file"><br/>
<input type="submit" name="submitform" class="btn btn-primary" value="Submit" style="width:20%;border-radius:0px;"><br/><br/>
</form>
</body>
</html>
UploadServlet.java
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
MultipartRequest m=new MultipartRequest(request,"d:/new");
out.print("successfully uploaded");
}
}
web.xmlの
<web-app>
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
</web-app>
ここにコードを貼り付ける方法を次回ご確認ください。 1つのコードブロックで複数のファイルではなく読みやすいはずです。正しくフォーマットしておく必要があります。私はあなたが新しいので、私は速い進歩を遂げました:)(私はコードatmをフォーマットすることができないので、他に誰かがいるようにしています)。 – CodeShark