私の目標は、画像をアップロードし、画像フォルダー内に Web コンテンツ フォルダーに保存することです。
{下の画像をご覧ください}
画像パスをデータベースに保存します
{下の画像をご覧ください}
このリンクで問題が発生しまし たが、画像を images/users フォルダーに保存できませんでした。
そして、保存パスが私の C ドライブに保存されていることに気付きました。パソコンを変えたら?イメージはそこにあるでしょうか?
以下は私のコードです。助けていただければ幸いです..ありがとう!:)
jspで
<input type="file" name="file">
サーブレット内
public class AServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String SAVE_DIR = "WebContent\\images\\users";
.........
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets absolute path of the web application
String appPath = request.getServletContext().getRealPath("");
// constructs path of the directory to save uploaded file
String savePath = appPath + File.separator + SAVE_DIR;
// creates the save directory if it does not exists
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
for (Part part : request.getParts()) {
String fileName = extractFileName(part);
part.write(savePath + File.separator + fileName);
}
System.out.println(savePath);
}
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}