I have tried
Part part = request.getPart("pimage1");
String fileNameAbsolute = extractFileName(part);
String fileName= extractFileNameEnd(fileNameAbsolute);
out.println("got fileName \n");
//String savePath ="E:\\ecom2\\WebContent\\images\\" + File.separator + fileName ;
String savePath ="ecom2//WebContent//images" + File.separator + fileName ;
System.out.println("filename is:"+fileName);
out.println("filename is:"+fileName);
String origPath="E:\\images\\"+ fileName ;
//String origPath=fileName ;
if(part!=null){
out.println("in part if");
copyFileUsingStream(origPath,savePath,out);
out.print("image 1 copied successfully \n");
}
method to extract filename which is at the end in filepath
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 "";
}
String extractFileNameEnd(String str) {
String[] items = str.split("\\\\");
int length= items.length ;
return items[length-1] ;
}
method to copy file
//CopyFile
private void copyFileUsingStream(String source, String dest,PrintWriter out) throws IOException {
File sourcef = new File(source);
File destf = new File(dest);
InputStream is = null;
OutputStream os = null;
out.println("In the copyFile method 5line");
try {
is = new FileInputStream(sourcef);
os = new FileOutputStream(destf);
out.println("after fileinputStream and outputstream");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
out.println("file copied successfully..");
}catch(Exception e){
out.println(e);
}
there is error file not found E:\images\"+ fileName <-- in this line I think server not understanding E:\ what is anathor way of uploading image/file to server I am making web app in java and uploading in aws
This example is working fine in localhost but not in real server
Aucun commentaire:
Enregistrer un commentaire