Vadim2004
asked on
Copy file from local drive to web server
Hello experts, I need your assistance:
my web app has to copy over a file from local (or network) drive to web-server.
file name will be saved to db.
Here is code - db part works fine file copy does not produce any errors but do not saves
file to location. Security on "/attachment" folder set to 775.
Please advise:
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String inputFile = "";
String outputFile = "";
ticketNum = 111111;
String fileName = "111111_021210030000_Test. txt";
try{
inputFile = "C:/Test.txt"; //req.getParameter("datafi le").trim( );
outputFile = "/path/attachments/111111_ 0212100300 00_Test.tx t";
copyFile(inputFile, outputFile);
}
catch(IOException ioe){
String error = "FileCopy+error+while+retr ieving+tic ket+attach ment:+" + ioe;
System.out.println(getDate ()+" "+error);
System.out.println(ioe.get Message()) ;
res.sendRedirect("/servlet /errorMsg? error="+ error);
}
//Save file name to DB
try{
con = cp.getConnection();
PreparedStatement statement = con.prepareStatement ("INSERT INTO ticket_attachment (ticket_num, tckt_attachment) VALUES (" + ticketNum + ", '" + fileName + "')");
statement.close();
cp.releaseConnection(con);
}
catch (Exception e){
String error = "Database+error+while+savi ng+attachm ent+to+dat abase+in+d oPost+meth od:+" + e;
System.out.println(getDate ()+" "+error);
System.out.println(e.getMe ssage());
res.sendRedirect("/servlet /errorMsg? error="+ error);
}
}
private void copyFile(String fileIn, String fileOut) throws IOException
{
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
File inputFile = new File(fileIn);
File outputFile = new File(fileOut);
inChannel = new FileInputStream(inputFile) .getChanne l();
outChannel = new FileOutputStream(outputFil e).getChan nel();
inChannel.transferTo(0, inChannel.size(), outChannel);
}
catch (IOException e) {
error = getDate () + "InputOutput+error+while+s aving+atta chment+fil e+: +" + e;
System.out.println(getDate () + " " + error);
System.out.println(e.getMe ssage());
res.sendRedirect("/servlet /errorMsg? error="+ error);
}
finally {
if (inChannel != null) {inChannel.close();}
if (outChannel != null) {outChannel.close();}
}
}
my web app has to copy over a file from local (or network) drive to web-server.
file name will be saved to db.
Here is code - db part works fine file copy does not produce any errors but do not saves
file to location. Security on "/attachment" folder set to 775.
Please advise:
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String inputFile = "";
String outputFile = "";
ticketNum = 111111;
String fileName = "111111_021210030000_Test.
try{
inputFile = "C:/Test.txt"; //req.getParameter("datafi
outputFile = "/path/attachments/111111_
copyFile(inputFile, outputFile);
}
catch(IOException ioe){
String error = "FileCopy+error+while+retr
System.out.println(getDate
System.out.println(ioe.get
res.sendRedirect("/servlet
}
//Save file name to DB
try{
con = cp.getConnection();
PreparedStatement statement = con.prepareStatement ("INSERT INTO ticket_attachment (ticket_num, tckt_attachment) VALUES (" + ticketNum + ", '" + fileName + "')");
statement.close();
cp.releaseConnection(con);
}
catch (Exception e){
String error = "Database+error+while+savi
System.out.println(getDate
System.out.println(e.getMe
res.sendRedirect("/servlet
}
}
private void copyFile(String fileIn, String fileOut) throws IOException
{
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
File inputFile = new File(fileIn);
File outputFile = new File(fileOut);
inChannel = new FileInputStream(inputFile)
outChannel = new FileOutputStream(outputFil
inChannel.transferTo(0, inChannel.size(), outChannel);
}
catch (IOException e) {
error = getDate () + "InputOutput+error+while+s
System.out.println(getDate
System.out.println(e.getMe
res.sendRedirect("/servlet
}
finally {
if (inChannel != null) {inChannel.close();}
if (outChannel != null) {outChannel.close();}
}
}
>> /path/attachments/111111_0 2121003000 0_Test.txt ";
What is this path ?
Is it a unix os path ?
What is this path ?
Is it a unix os path ?
ASKER
yes this is path on unix web server
ASKER
Good morning experts, is any suggestion on it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Actually I am using <input type=file> , or it does not make a difference and I have to use File's input stream?
ASKER