I get a text file via:
JFileChooser dialog = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Текстовый файл", "txt");
dialog.removeChoosableFileFilter(dialog.getFileFilter());
dialog.addChoosableFileFilter(filter);
dialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
dialog.setDialogTitle("Выберите текстовый файл");
dialog.setDialogType(JFileChooser.OPEN_DIALOG);
dialog.setMultiSelectionEnabled(false);
int ret = dialog.showDialog(null, "Открыть");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = dialog.getSelectedFile();
pach = file.getAbsolutePath();
} else return;
System.out.println(pach);
Last command shows:
D:\data\streets.txt
Now I make a request:
try {
querySQL = "LOAD DATA LOCAL INFILE '" + pach + "' INTO TABLE " + DB_NAME + "." + TABLE_NAME + ";";
stSQL.execute(querySQL);
} catch (SQLException e) {
ErrorMsg(e, querySQL);
isError = true;
break;
}
And I do my ErrorMsg issues:
Unable to process the query:
LOAD DATA LOCAL INFILE 'D:\data\streets.txt' INTO TABLE base2.streets;
java.sql.SQLException: Unable to open file 'D:datastreets.txt'for 'LOAD DATA LOCAL INFILE' command.Due to underlying IOException:
Where it removes the skew, and why all this is happening? In fact, if such a request is inserted into Workbench, the query is executed without error. Please tell me the solution of this problem, it is very necessary. Thank you in advance.
P.S. The text is translated into English by Google Translate
You may want to escape the backslashes contained in your path. For you database a backslash ( \ ) can change the value in a way that the string cannot be saved in the database. This can be a problem with other special characters like ' also. Concatenating a string containing ' to you query, adds an additional ' which will break the query.
You can use StringEscapeUtils for doing this. https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
Related
We are currently using the EasyUpload add-on, and we have specified the criteria for this component:
a) only CSV files are allowed, with a cap size of 1MB per file.
b) only one file can be submitted at a time.
We just did an uploading test on small-sized CSV files that are below 100Kb. Usually, the upload process completes successfully. Occasionally, the error of "Could not open file, The system cannot find the file specified" is displayed although the file is already inside the temp folder, and we found that this happens either when:
a) If the same file is uploaded again after making a small change and within a few seconds after the file has been uploaded successfully.
b) If there are two tabs of the web app, logged under different users were uploading their respective csv files and they also do the same thing of changing values in the csv before uploading them again.
We tried forcing the file upload through (as another testing method) and noticed after a while that the files sometimes get stuck in the queue although we have imposed a one file at a submission time rule. It was displayed in a message "There are too many files over the count limit". We also considered of putting a sleep / wait command of 3-5 seconds after the file submission.
MultiFileUpload multiFileUpload = new MultiFileUpload() {
#Override
protected void handleFile(File tmpFile, String fileName, String mimeType, long length) {
String[] header = {"EOD_NUM","OUTLET_NAME","POSM_NAME","EOD_DATE","TOTAL_SALES","GROSS_SALES",
"TRAN_COUNT","VOID_COUNT","SERVICE_CHARGE","DISCOUNT_AMT","VAT_TAX_AMT","SVC_TAX_AMT","ROUNDING_ADJ"};
uploadLogger.debug("File: " + tmpFile.getAbsolutePath());
uploadLogger.debug("FileName: " + fileName);
uploadLogger.debug("MimeType: " + mimeType);
uploadLogger.debug("File Length: " + length);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
LocalDateTime now = LocalDateTime.now();
File f2 = null;
f2 = new File(busId+"_"+dtf.format(now)+".csv");
tmpFile.renameTo(f2);
try {
///var/lib/tomcat8/ in linux
///D:\\home\\site\\wwwroot\\ in Windows
uploadLogger.debug("f2 absolutepath: " + f2.getAbsolutePath());
uploadLogger.debug("f2 canonical path: " + f2.getCanonicalPath());
CloudBlockBlob blob = container.getBlockBlobReference(f2.getName());
if(f2.length() > 0){
blob.uploadFromFile(f2.getAbsolutePath());
Notification.show("File upload completed.",Notification.Type.TRAY_NOTIFICATION);
}
CSVReader reader = new CSVReader(new FileReader(f2.getAbsolutePath()), ',' , '"' , 0);
//read header name
//String[] myheader = reader.readNext();
//NOTE :: Store all row and column from csv info List of String Array
myEntries = reader.readAll();
if (myEntries != null && !myEntries.isEmpty()) {
boolean success = uploadDAO.insertUploaderEntry(myEntries,busId, userId,"");
uploadLogger.debug("SUCCESSS??? " + success);
if(success){
Notification successNotify = new Notification("Record has been created successfully.","Upload Successful!");
successNotify.setDelayMsec(3000);
successNotify.setStyleName(ValoTheme.NOTIFICATION_SUCCESS);
successNotify.setPosition(Position.MIDDLE_CENTER);
successNotify.show(Page.getCurrent());
}else {
Notification.show("Error in submitting uploaded record.","Upload failed!"
, Notification.Type.ERROR_MESSAGE).setDelayMsec(3000);
}
Thread.sleep(3000); //added to see if the delay solves the problem or not.
}
} catch (URISyntaxException | StorageException | IOException ex) {
new Notification("Could not open file",ex.getMessage(),Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());
uploadLogger.debug(ex);
} catch (InterruptedException ix) {
uploadLogger.debug("Interrupted Exception found: " + ix.getMessage());
}
}
#Override
protected boolean supportsFileDrops() {
return false;
}
};
multiFileUpload.setMaxFileCount(1);
multiFileUpload.setUploadButtonCaption("Upload CSV file here");
multiFileUpload.setMaxFileSize(fileSizeLimit); // 2MB
multiFileUpload.setAcceptFilter(".csv");
We are unsure whether if this problem is a known limitation of the component or not.
Some of the questions we have discovered along the way are:
a) Is there a better way or to control on the file uploading and to avoid the open file / file not found error?
b) Are the values in the setAcceptedFilter method the mime/type values or something else. We noticed for images, it's "images/*" but for csv, we had to put in as ".csv"
Answering to your second question. The acceptFilter is directly passed to upload inputs "accept" attribute, so both .csv and text/csv should do fine. See https://www.w3schools.com/tags/att_input_accept.asp for more instructions.
Fortify SCA tool find an issue called Portability Flaw: File Separator, but with the source of those issues, there is none hardcoded file separator such as "/" or "\", only file extensions such as "." exists.
Our customer used Fortify SCA to scan their legacy system source codes. Fortify found Portability Flaw: File Separator issues. It said that the file names which declared in a String array contain hard-coded file separator (this string array is the source of the problem), but I can't see any file separator such as "/" or "\" in those file name strings.
public static final String SYS_SPRAT = File.separator; //this is declared as a class attribute
String[] fileNames = { //fortify points out here is the source of this issue
"",
"2.5.1aaaaa.pdf",
"2.5.2bbbbb.pdf",
"2.5.3ccccc.pdf",
.......
"5.1.4甲甲甲甲甲.pdf",
};
String fileName = null;
File file = null;
int iParam = Integer.parseInt(sParam);
if (iParam >= 1 && iParam <= 26) {
fileName = fileNames[iParam];
String filePath = SYS_SPRAT + "home" + SYS_SPRAT + "xxx" + SYS_SPRAT + "ooo" + SYS_SPRAT + "Resource" + SYS_SPRAT + fileName;
file = new File(filePath);
else {
addFacesMessage("wrong parameter");
return null;
}
I still can't figure out why there is an issue. Is it a false positive? (but why?)
It seems that Fortify may be overly strict here. Even their website says that using File.separator like this should be ok.
I can't see any portability problem using File.separator. Even on OpenVMS systems, where file paths are in the format devicename:[directory.subdirectory]file.ext;version, the Java runtime internally translates between a / separator and the proper VMS format.
First, double-check using a "Find" tool that you don't have any \ or / characters in any of the strings in filenames[] (don't just rely on visual inspection). If there is definitely no such character, then proceed with the suggestion below.
Try avoiding File.separator altogether. Instead, try using Paths.get:
public static final Path RESOURCE_DIR = Paths.get(
"home", "xxx", "ooo", "Resource");
String[] fileNames = {
"",
"2.5.1aaaaa.pdf",
"2.5.2bbbbb.pdf",
"2.5.3ccccc.pdf",
.......
"5.1.4甲甲甲甲甲.pdf",
};
String fileName = null;
File file = null;
int iParam = Integer.parseInt(sParam);
if (iParam >= 1 && iParam <= 26) {
fileName = fileNames[iParam];
file = RESOURCE_DIR.resolve(filePath).toFile();
else {
addFacesMessage("wrong parameter");
return null;
}
Is Fortify ok when you do this?
So I'm looking to find a way to import data from a .txt file into my mysql table using a java desktop application, I wrote these lines of code but when I run my program I can see the Jform and the first button work properly, but when I press the second one, nothing happens. What should I change?
private void ouvrirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f=chooser.getSelectedFile();
filename=f.getAbsolutePath();
chemin.setText(filename);
}
private void chargerActionPerformed(java.awt.event.ActionEvent evt) {
String tablename = "tmp";
try{
stm=maConnexion.ObtenirConnexion().createStatement();
stm.executeUpdate("LOAD DATA LOCAL INFILE \"" + filename + "\" INTO TABLE " + tablename);
JOptionPane.showMessageDialog(null,"Fichier trace chargé avec succés");
}
catch(SQLException ex){
System.err.println(ex);
}
For clarifying your situation, you can run the command in your mysql console. If succeeds, then you can try it in your code.
First try,
LOAD DATA LOCAL INFILE '/tmp/foo.txt'
INTO TABLE tmp;
Second try,
LOAD DATA LOCAL INFILE '/tmp/foo.txt'
INTO TABLE tmp COLUMNS TERMINATED BY '\t';
Third try,
LOAD DATA LOCAL INFILE '/tmp/foo.txt' INTO TABLE tmp
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Forth try,
LOAD DATA LOCAL
INFILE '/tmp/mydata.txt' INTO TABLE tmp
COLUMNS TERMINATED BY '\t' ## This should be your delimiter
OPTIONALLY ENCLOSED BY '"'; ## ...and if text is enclosed, specify here
From those, which runs perfectly, you can implement your code.
For more,
You can go through MySQL manual
Resource Link:
LOAD DATA INFILE Syntax
I'm trying to programmatically import a .sql file into MySQL. The .sql file was generated by mysqldump. I'm trying to do this dynamically in a Java program. But, it keeps failing on the "<" character within Java (I think). If I grab the String in the debugger of the command it's about to run (the "combined" variable below), and paste it into the command line, it works fine. Likewise, when I was trying to get the mysqldump working inside this program, it failed on the ">" character, and I had to replace it with the "--result-file=" argument to get it to work.
String command = mySqlPath + "mysql.exe";
String user = "-u " + settings.dbUser;
String password = "-p" + settings.dbPassword;
String db = settings.dbDatabase;
String inputFile = filePath + mySqlDumpFile;
String combined = command + " " + user + " " + password + " " + db + " < " + inputFile;
ExternalCommandExecuter ece = new ExternalCommandExecuter(combined);
int code = ece.execute();
This results in this String for example
C:\software\mysql5\bin\mysql.exe -u root -p<password>
db_name < C:\software\tomcat7\webapps\ROOT\WEB-INF\documents\dump-1461789460425.sql
Which will result in a exitCode of 1. Pasting it into the command line, and it'll work.
So I couldn't get it to work in Java by importing the file with a "<", instead I had to write an external .sh/.bat file which I call from my Java code.
I have a java program that call my Perl script to upload a file. It has a file parameter to the Perl script that contain the location of file to upload.
public static void legacyPerlInspectionUpload(String creator, String artifactId, java.io.File uploadedFile, String description ) {
PostMethod mPost = new PostMethod(getProperty(Constants.PERL_FILE_URL) + "inspectionUpload.pl");
try {
String upsSessionId = getUpsSessionCookie();
//When passing multiple cookies as a String, seperate each cookie with a semi-colon and space
String cookies = "UPS_SESSION=" + upsSessionId;
log.debug(getCurrentUser() + " Inspection File Upload Cookies " + cookies);
Part[] parts = {
new StringPart("creator", creator),
new StringPart("artifactId", artifactId),
new StringPart("fileName", uploadedFile.getName()),
new StringPart("description", description),
new FilePart("fileContent", uploadedFile) };
mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost.getParams()));
mPost.setRequestHeader("Cookie",cookies);
HttpClient httpClient = new HttpClient();
int status = httpClient.executeMethod(mPost);
if (status == HttpStatus.SC_OK) {
String tmpRetVal = mPost.getResponseBodyAsString();
log.info(getCurrentUser() + ":Inspection Upload complete, response=" + tmpRetVal);
} else {
log.info(getCurrentUser() + ":Inspection Upload failed, response=" + HttpStatus.getStatusText(status));
}
} catch (Exception ex) {
log.error(getCurrentUser() + ": Error in Inspection upload reason:" + ex.getMessage());
ex.printStackTrace();
} finally {
mPost.releaseConnection();
}
}
In this part of my Perl script, it get the information about the file, read from it and write the content to a blink file in my server.
#
# Time to upload the file onto the server in an appropropriate path.
#
$fileHandle=$obj->param('fileContent');
writeLog("fileHandle:$fileHandle");
open(OUTFILE,">$AttachFile");
while ($bytesread=read($fileHandle,$buffer,1024)) {
print OUTFILE $buffer;
}
close(OUTFILE);
writeLog("Download file, checking stats.");
#
# Find out if the file was correctly uploaded. If it was not the file size will be 0.
#
($size) = (stat($AttachFile))[7];
Right now the problem is this only work for file with no space in its name, otherwise $size is 0. I was reading online and it seems both Java file and Perl filehandle work with space, so what am I doing wrong?
Your poor variable naming has tripped you up:
open(OUTFILE,">$AttachFile");
^^^^^^^---this is your filehandle
while ($bytesread=read($fileHandle,$buffer,1024)) {
^^^^^^^^^^^--- this is just a string
You're trying to read from something that's NOT a filehandle, it's just a variable whose name happens to be "filehandle". You never opened up the specified file for reading. e.g. you're missing
open(INFILE, "<$fileHandle");
read(INFILE, $buffer, 1024);