0
我想從struts操作上傳文件。我在行動需要的路徑,我的文件夾:如何將文件上載到struts 2中的特定文件夾中
我嘗試使用
String contextPath = request.getContextPath();
,但我發現java.lang.NullPointerException
我想從struts操作上傳文件。我在行動需要的路徑,我的文件夾:如何將文件上載到struts 2中的特定文件夾中
我嘗試使用
String contextPath = request.getContextPath();
,但我發現java.lang.NullPointerException
無論是店卡塔利娜這是父文件夾到項目文件夾
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "yourfolderName");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
java.util.Date date= new java.util.Date();
String Path = dir.getAbsolutePath() + File.separator + (new Timestamp(date.getTime())).toString().replace(":", "").toString().replace(".", ".").toString().replace(" ","").toString().replace("-","").toString()+".pdf";
或者在您的項目中創建一個文件夾並在那裏存儲。
if (!file.isEmpty()) {
//filter for checking file extewnsion
if(file.getContentType().equalsIgnoreCase("image/jpg") || file.getContentType().equalsIgnoreCase("image/jpeg")){
//if file is >2 MB or < 2MB
double size = file.getSize();
double kilobytes = (size/1024);
double megabytes = (kilobytes/1024);
if(megabytes<2){
try {
byte[] bytes = file.getBytes();
String filePath = request.getRealPath("/")+"yourFolderName\\ProfileImages\\"+SessionManagement.getUserName()+".jpg";
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(filePath)));
stream.write(bytes);
stream.close();
//console call
}
else{
model.put("error", "Please select File less than 2 MB");
return new ModelAndView("uploadPhotoTile");
}
}else{
model.put("error", "Please select JPEG File");
return new ModelAndView("uploadPhotoTile");
}
} else {
model.put("error", "Please select File");
return new ModelAndView("uploadPhotoTile");
}
要下載文件? –
不,我想將其上傳到WebContent中的特定文件夾 –
請說明您的具體問題或添加其他詳細信息,以確切地突出顯示您需要的內容。正如目前所寫,很難確切地說出你在問什麼。請參閱「如何問問」頁面以獲取有關澄清此問題的幫助。 –