This question already has an answer here:
这个问题在这里已有答案:
- Recommended way to save uploaded files in a servlet application 2 answers
建议的方法来保存上传的文件在servlet应用程序2答案
My goal is to upload an image and save inside images folder to Web Content Folder.
我的目标是上传图像并将图像文件夹保存到Web内容文件夹。
{Please see the image below}
{请参阅下面的图片}
and save the image path into database
并将图像路径保存到数据库中
{Please see the image below}
{请参阅下面的图片}
I encountered some problem at this link I tried to do but I did not managed to save the image to images/users folder.
我在尝试这个链接时遇到了一些问题,但我没有设法将图像保存到images / users文件夹。
And I realize that the save path is store at my C DRIVE. What if I change my computer? Will the image will be there?
我意识到保存路径存储在我的C DRIVE中。如果我更换电脑怎么办?图像会在那里吗?
Below are my codes. Help will be appreciate.. Thanks! :)
以下是我的代码。帮助将是欣赏..谢谢! :)
In jsp
<input type="file" name="file">
In servlet
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 "";
}
1 个解决方案
#1
0
Follow the link provided by SasiKathimanda for code implementation.
按照SasiKathimanda提供的链接进行代码实现。
Create the file name as 'OrignalFilename+SystemTimeInMills' in order to avoid overriding already uploaded file with same filename.
将文件名创建为“OrignalFilename + SystemTimeInMills”,以避免覆盖已上载的文件具有相同的文件名。
Otherwise check for duplicate filename before write filecontent to server.
否则在将filecontent写入服务器之前检查重复的文件名。
Follow the pattern(For DB File Path) if you need to provide download option(later), otherwise you can save the uploaded absolute file path.
如果需要提供下载选项(稍后),请遵循模式(对于DB文件路径),否则可以保存上载的绝对文件路径。
#1
0
Follow the link provided by SasiKathimanda for code implementation.
按照SasiKathimanda提供的链接进行代码实现。
Create the file name as 'OrignalFilename+SystemTimeInMills' in order to avoid overriding already uploaded file with same filename.
将文件名创建为“OrignalFilename + SystemTimeInMills”,以避免覆盖已上载的文件具有相同的文件名。
Otherwise check for duplicate filename before write filecontent to server.
否则在将filecontent写入服务器之前检查重复的文件名。
Follow the pattern(For DB File Path) if you need to provide download option(later), otherwise you can save the uploaded absolute file path.
如果需要提供下载选项(稍后),请遵循模式(对于DB文件路径),否则可以保存上载的绝对文件路径。