linux下上傳檔案,檔案上傳不上去
起初發現問題,以為是程式碼出錯,於是排查程式碼。檢查上傳路徑
System.out.println(":::::::::匯入資料::::::::");
System.out.println("uploadFileName::"+this.myFileFileName);
System.out.println(optionValue);//頁面下拉框傳進的值
String myFileFileNameNew = new Date().getTime() + "_" + myFileFileName;// 毫秒數+檔案姓名
String targetPath = ServletActionContext.getServletContext().getRealPath("/")+"uploadtelnumber"+File.separator+myFileFileNameNew;
System.out.println(targetPath);
File targetFile = new File(targetPath);
copyFile(myFile, targetFile);
//*********************************
public void copyFile(File src, File target) {
InputStream is = null;
OutputStream os = null;
byte[] number = new byte[BUFFER_SIZE];
try {
is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
os = new BufferedOutputStream(new FileOutputStream(target),
BUFFER_SIZE);
while (is.read(number) > 0) {
os.write(number);
}
os.close();
is.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
結果發現沒有問題,但是依然上傳不上去檔案,後發現是由於linux下的檔案有許可權控制原因,於是把伺服器上傳的專案資料夾,設定為可讀可寫的操作,
進入到ls可以看到的上傳資料夾目錄後,執行chmod 777 fileurl / (chmod 777 file),問題解決