Uploading file to linux server in java - java

I have to create a web application in java to upload all types of file.War file will be deployed on the linux server.The problem is path.Everything is working on localhost but when it deployed to linux server its not working,the page became empty.The below is my code:
try {
dbConnectionUtils db = new dbConnectionUtils();
Connection conn = null;
try {
conn = db.getConnection();
} catch (ClassNotFoundException ex) {
Logger.getLogger(upload_images.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(upload_images.class.getName()).log(Level.SEVERE, null, ex);
}
Map<String, String> paramMap = new LinkedHashMap<String, String>();
InputStream filecontent = null;
FileItem modelFile = null;
DiskFileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(50 * 1024 * 1024);
// Parse the request
List<FileItem> items = upload.parseRequest(request);
// List<FileItem> items = new ServletFileUpload(new
// DiskFileItemFactory()).parseRequest(req);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input
// type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
// ... (do your job here)
// System.out.println("TESTING " + fieldname + " : "
// + fieldvalue);
paramMap.put(fieldname, fieldvalue);
} else {
// Process form file field (input type="file").
String fieldname = item.getFieldName();
String filename = FilenameUtils.getName(item.getName());
modelFile = item;
System.out.println("ModelFile"+modelFile);
filecontent = item.getInputStream();
// ... (do your job here)
// System.out.println("TESTING *****" + fieldname + " : "
// + filename + " : " + item.getName());
paramMap.put(fieldname, filename);
}
}
System.out.println("filemanager");
String filemanager = paramMap.get("filemanager");
if ((filemanager != null) && (filemanager.equalsIgnoreCase("Submit"))) {
try {
System.out.println("Inside");
String filename = paramMap.get("filename");
String file = paramMap.get("file");
System.out.println("........." + file);
String prj = paramMap.get("PjList");
if (null != filename && null != file) {
String relative = getRealpath() + "filemanager\\" + request.getSession(true).getAttribute("logged_user_id") + "\\";
File mkTime = new File(relative);
mkTime.mkdirs();
String ipath = uploadData(modelFile, relative);
FileInputStream fis = new FileInputStream(ipath);
Statement st = conn.createStatement();
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI();
//create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
//open the large object for write
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0) {
obj.write(buf, 0, s);
tl += s;
}
// Close the large object
obj.close();
int f=8;
String fileType = modelFile.getContentType();
}

does the user/owner on the hosting server have permissions to write to the folders/create a subfolder in the PWD you are running your WAR from? CHMOD 777 your WARs folder and see if it lets you create the relative subdir needed to run.
If that is not the case the reason you are not seeing an error is you only defined ClassNotFoundException and SQLException errors and not a more general error catch to report the access issues that I think you might be having.
Put a generic error catch around your try{} and print the error, I think you'll get some sort of access error.

You should always use "/" as path separators in Java.
"\" only works on Windows.

Related

Java - Download file from URL with matching file name pattern

I want to download few files from a URL. I know the starting of the file name. But the next part would be different. Mostly a date. But it could be different for different files. From Java code, is there any way to download file with matching pattern?
If I hit the below URL in chrome, all the files are listed and I have to download the required files manually.
http://<ip_address>:<port>/MR/build/report/scan/daily/2021-12-13_120/data/
File names can b like below. It will have known file name and date. The date can be different. Either the same as in URL or some older one.
scan_report_2021_12_13_120.txt
build_report_2021_12_10_110.txt
my_reportdata_2021_11_30_110.txt
As of now, my Java code is like below. I have to pass the complete URL with exact file name to download the files. Most of the cases it would be same as the date and number in URL. So in the program I take the date part from URL and add it to my file name nd pass as the URL. But for some files it might change and for those I have to manually download.
private static void downloadFile(String remoteURLPath, String localPath) {
System.out.println("DownloadFileTest.downloadFile() Downloading from " + remoteURLPath + " to = " + localPath);
FileOutputStream fos = null;
try {
URL website = new URL(remoteURLPath);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
fos = new FileOutputStream(localPath);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The argument remoteURLPath is passed like http://<ip_address>:<port>/MR/build/report/scan/daily/2021-12-13_120/data/scan_report_2021_12_13_120.txt
And localPath is passed like C:\\MyDir\\MyData\\scan_report_2021_12_13_120.txt
Similarly other files also with date as 2021_12_13_120. Other files wont get downloaded. But will create empty file in the same directory which I will delete later since size is 0.
Is there any way we can pass pattern here?
Like http://<ip_address>:<port>/MR/build/report/scan/daily/2021-12-13_120/data/scan_report_*.txt
And instead of passing complete local path, is there any way to pass only directory where the file should get downloaded with exact same name as in the remote system?
In Linux I can use wget with pattern matching. But was looking for Java way to download in all platforms.
wget -r -np -nH --cut-dirs=10 -A "scan_report*.txt" "http://<ip_address>:<port>/MR/build/report/scan/daily/2021-12-13_120/data/"
Thanks to comment from #FedericoklezCulloca. I modified my code using this answer
The solution I did is read all html page and get all href values as it had only the file names with extension. From there I had another list which I used to get the matching files and those I downloaded then using my code in the Question.
Method to get all href list from URL. may be optimisation can be done. Also I did not use any extra library.
private static List<String> getAllHREFListFromURL(String downloadURL) {
URL url;
InputStream is = null;
List<String> hrefListFromURL = new ArrayList<>();
try {
url = new URL(downloadURL);
is = url.openStream();
byte[] buffer = new byte[1024];
int bytesRead = -1;
StringBuilder page = new StringBuilder(1024);
while ((bytesRead = is.read(buffer)) != -1) {
String str = new String(buffer, 0, bytesRead);
page.append(str);
}
StringBuilder htmlPage = new StringBuilder(page);
String search_start = "href=\"";
String search_end = "\"";
while (!htmlPage.isEmpty()) {
int indexOf = htmlPage.indexOf(search_start);
if (indexOf != -1) {
String substring = htmlPage.substring(indexOf + search_start.length());
String linkName = substring.substring(0, substring.indexOf(search_end));
hrefListFromURL.add(linkName);
htmlPage = new StringBuilder(substring);
} else {
htmlPage = new StringBuilder();
}
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
}
}
return hrefListFromURL;
}
Method to get list of files that I needed.
private static List<String> getDownloadList(List<String> allHREFListFromURL) {
List<String> filesList = getMyFilesList();
List<String> downloadList = new ArrayList<>();
for (String fileName : filesList) {
Predicate<String> fileFilter = Pattern.compile(fileName + "*").asPredicate();
List<String> collect = allHREFListFromURL.stream().filter(fileFilter).collect(Collectors.toList());
downloadList.addAll(collect);
}
return downloadList;
}
private static List<String> getMyFilesList() {
List<String> filesList = new ArrayList<>();
filesList.add("scan_report");
filesList.add("build_report");
filesList.add("my_reportdata");
return filesList;
}
The downloadList I iterate and uses my original download method to download.

file got corrupted during blob to file conversion (java)

why my download file always got corrupted.
I have a code that will upload file using blob.
the code is
InputStream inputStream = new FileInputStream(new File(filePath)); //the file to upload
pst.setBinaryStream(15, inputStream); //to upload the selected file
it's successfully upload to my sql data base but when I try to download it, it always got corrupted.
below is my code to download the file from sql.
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url1 = "jdbc:sqlserver://ets88-spare:1433;databaseName=users;user=sa;password=test";
Connection conn1 = DriverManager.getConnection(url1);
String query1 = "SELECT * FROM ets_verification";
Statement state = conn1.createStatement();
ResultSet rs = state.executeQuery(query1);
while (rs.next())
{
String boardname = jboardName.getSelectedItem().toString();
String sn = jserialNumber.getText();
String status = jverificationStatus.getSelectedItem().toString();
String filename = boardname + "_" + "_" + sn + "_"+ status;
byte[] array = rs.getBytes(16);
FileOutputStream fos = new FileOutputStream("c:\\" + filename + ".rar");
fos.write(array);
fos.close();
System.out.println("array:" + array);
}
}
catch (ClassNotFoundException | SQLException e)
{
jnote.setText(e.toString());
System.out.println("error" + e.toString());
} catch (IOException ex) {
Logger.getLogger(ets_verification.class.getName()).log(Level.SEVERE, null, ex);
}
You could try.
Tested in a ORACLE DB.
(...)
Blob blob = rs.getBlob(16);
if (blob != null) {
byte[] b = blob.getBytes(1, (int) blob.length());
try (FileOutputStream fos = new FileOutputStream("PATH/TO/FILE/FILENAME.rar")){
fos.write(file);
}catch (IOException ex) {
//Do something
}
}
(...)

Importing CSV into MySQL through JAVA

So I'm trying to import a CSV file into my MySQL database through my Java program. The program imports everything that's in the file, like it's suppose to, but the first row, it send to the end of the table, and the program see it's there, but if I search for that nr, it says it doesn't exists. And if I go directly to the database table and edit the nr(if the nr is 137, and I edit and write 137 again) the program recognize that nr, and if I search for it, it will find, and the database table organizes itself and sends that entry where is suppose to be.
I just don't see any logic in this. I someone could help me out, I'd appreciated.
LOAD DATA INFILE 'C:\\Users\\carla.DESKTOP-9364K9K\\Desktop\\Alunos_1.csv'
INTO TABLE utentes character set utf8
FIELDS TERMINATED BY ','
(NrProcesso, Nome, #Nome_Resumido, Ano, Turma, #Subsidio, #Nome_EE, #NIF, #email, #Obs)
SET
Subsidio = IF(#Subsidio='','Nenhum',#Subsidio),
Nome_Resumido = IF(#Nome_Resumido='',NULL,#Nome_Resumido),
Nome_EE = IF(#Nome_EE='',NULL,#Nome_EE),
NIF = IF(#NIF = '', NULL,#NIF),
email = IF(#email='',NULL,#email),
Obs = IF(#Obs='',NULL,#Obs);
Thanks in advance.
You have do do something to check cell/column value and form a sql to inject in MySQL.
public List<Object> getRecordingsListFromCsv(String csvFileLocation, String mp3FileLocation, String mp3FileLocation2, String saveFileLocation, ChannelSftp sftp) {
Map<String, File> recordingsFilesMap = null;
BufferedReader br = null;
List<String> errorFilesList = new ArrayList<>();
List<Object> tempList = new LinkedList<>();
try {
csvRows = 0;
recordingsFilesMap = new LinkedHashMap<>();
br = new BufferedReader(new FileReader(csvFileLocation));
String line = br.readLine();
scriptLog.info("\n" + csvFileLocation + " loaded. Parsing File...");
while ((line = br.readLine()) != null) {
String[] csvArray = parseCsvLineToArray(line);
// System.out.println(Arrays.asList(csvArray) + "\n\n");
if (csvArray[0].trim().isEmpty()) {
continue;
}
/* Do your stuff here */
csvRows++;
}
} catch (FileNotFoundException e) {
scriptLog.error("\n---ERROR---\n FILE NOT FOUND: " + csvFileLocation);
String errorStr = "Type=" + e.toString();
errorStr += "StackTrace=" + Arrays.toString(e.getStackTrace());
scriptLog.error(errorStr);
} catch (IOException e) {
String errorStr = "Type=" + e.toString();
errorStr += "StackTrace=" + Arrays.toString(e.getStackTrace());
scriptLog.error(errorStr);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
Hope it will help you at some extent!!

StreamResource, ByteArray problems

I cant understand why my code are not running all the time.
I am opening a jasper report but for first 4 opening times the report is cached or code are not executing (Code in the new StreamResource are not executing first 4 times). new StreamResource.StreamSource() are running only at 5 time WHY ? The first 4 times i got the old,cached,temp or i event dont know what a pdf file with old params.
maybe someone know the issue ?
public static void open(final String fileName, final HashMap<String, Object> data ) {
mylog.pl("### Param's print # open Report: Filename:" + fileName);
try {
Iterator<?> i = data.keySet().iterator();
while (i.hasNext()) {
String id = i.next().toString();
String value = (data.get(id) != null) ? data.get(id).toString() : "null";
mylog.pl(" id: " + id + " value: " + value);
}
} catch (Exception e) {
e.printStackTrace();
mylog.pl(e.getMessage());
}
StreamResource.StreamSource source = null;
source = new StreamResource.StreamSource() {
public InputStream getStream() {
byte[] b = null;
InputStream reportStream = null;
try {
reportStream = new BufferedInputStream(new FileInputStream(PATH + fileName + JASPER));
b = JasperRunManager.runReportToPdf(reportStream, data, new JREmptyDataSource());
} catch (JRException ex) {
ex.printStackTrace();
mylog.pl("Err # JR" + ex.getMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
Utils.showMessage(SU.NOTFOUND);
return null;
}
return new ByteArrayInputStream(b);
}
};
StreamResource resource = null;
resource = new StreamResource(source, fileName + PDF);
resource.setMIMEType("application/pdf");
Page p = Page.getCurrent();
p.open(resource, "Report", false);
}
Here is the answer
I all the time used resource.setCacheTime(0); but really needed resource.setCacheTime(1000); because
In theory <= 0 disables caching. In practice Chrome, Safari (and,
apparently, IE) all ignore <=0.

struts how to set specific name to uploaded file

i want to give specific name to the uploaded file in my destination folder.. this is my action file code.. here i want to give name like CPIC_1.jpg,CPIC_2.jpg,CPIC_3.jpg,CPIC_4.jpg etc but every time it is assigning name : CPIC_1.jpg.. so how i declare variable ext so that through out it will be distinct..
CommercialFileBean b = (CommercialFileBean) form;
FormFile f = b.getF();
String s = request.getParameter("action");
HttpSession session = request.getSession(false);
String n = (String) session.getAttribute("str");
String email = session.getAttribute("uname").toString();
String status = (String) session.getAttribute("status");
String type = request.getParameter("type");
String pid;
long ext=0;
int id;
if (s.equalsIgnoreCase("finish")) {
return mapping.findForward(next);
} else { /// first else
String a = getServlet().getServletContext().getRealPath("/");
File file = new File(a + "uploaded/CPIC_"+ ++ext+".jpg");
if (!file.exists()) {
FileOutputStream out = new FileOutputStream(file);
out.write(f.getFileData());
out.close();
}
try {
if (n.equalsIgnoreCase("rent")) {
Session sess = UtilClass.createSession();
Transaction tx = sess.beginTransaction();
if (status.equalsIgnoreCase("new")) {
String sql1 = "select MAX(id) from Rentcommercialrecord where loginid=:email";
Query q1 = sess.createQuery(sql1);
q1.setParameter("email", email);
// JOptionPane.showMessageDialog(null, "max id is :");
List<Rentcommercialrecord> l = q1.list();
Rentcommercialrecord rc = l.get(l.size()-1);
id = rc.getId();
} else {
pid = (String) session.getAttribute("id");
id = Integer.parseInt(pid);
}
JOptionPane.showMessageDialog(null, " latest id is :" + id);
if (type.equalsIgnoreCase("frontpic")) {
try {
String file1 = f.getFileName();
JOptionPane.showMessageDialog(null, "file name is : "+file1);
Rentcommercialrecord rc1 = (Rentcommercialrecord) sess.get(Rentcommercialrecord.class, id);
rc1.setImg1("CPIC_" +ext+".jpg");
sess.update(rc1);
// JOptionPane.showMessageDialog(null, "img1");
} // img1 try ends
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Second error is : " + e.getMessage());
}
} // fontpic if ends
else {
try {
String file1 = f.getFileName();
JOptionPane.showMessageDialog(null, "file name is : "+file1);
Rentcommercialrecord rc1 = (Rentcommercialrecord) sess.get(Rentcommercialrecord.class, id);
rc1.setImg2("CPIC_" +ext+".jpg");
sess.update(rc1);
// JOptionPane.showMessageDialog(null, "img2");
} // img2 try ends
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Second error is : " + e.getMessage());
}
} // else img2 ends
// l.size if ends
tx.commit();
}
Make your variable ext as static.
static long ext = 0;
This will make the variable common to all instances.
Note : You need to store this value somewhere in db / file during restart and get it during application startup to make it consistent irrespective of restart of your application
You can make your ext variable static
Note: The scope of your static variable is for the current class Loader. ie, if it is a diff class loader is used , this will change.
other option is store the ext value in session and every time you upload a new image file, get this value from session and use it. And you need to put the new value back into session as well. This approach will work for per user. ie if your application is having diff users, for diff users it will have diff value based on session
You can use a static variable, but it won't be consistent through application restarts.
I would change approach and would read filenames, then extracting the numbers from their names, getting the highest, incrementing it and then writing a new file.
Use Apache Commons to avoid reinventing the wheel.
Kickoff example:
String path = getServlet().getServletContext().getRealPath("/") + "uploaded/";
String partialName = "CPIC_";
int markerLength = partialName.length();
int maxValue = 0;
// Find all files, if any, with name starting with "CPIC_" in the desired folder
List<File> files = FileUtils.listFiles(new File(path),
new PrefixFileFilter(partialName),
null);
if (!files.isEmpty()){
for (File file : files) {
// Strip marker and extension
int num = Integer.parseInt(
file.getName().substring(markerLength,
file.getName().indexOf("."))
);
// compare the number, if greater, set as new max value
if (num > maxValue) {
maxValue = num;
}
}
}
String newFile = partialName + ++maxValue + ".jpg";
System.out.println("Next file name would be : " + newFile);

Categories

Resources