Java file upload : cannot find symbol - getSubmittedFileName() [duplicate] - java

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed last month.
I'm trying to retrieve a file from a JSP form.
I'm using Tomcat 9.0 / Java 8
When I compile, I got the messages :
addHabit.java:56: error: cannot find symbol
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
symbol: method getSubmittedFileName()
location: variable filePart of type Part
addHabit.java:56: error: cannot find symbol
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
symbol: variable Paths
My code is pretty simple and looks like everything I found here and on the web :
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import com.microsoft.sqlserver.jdbc.*;
// Appelé par le JSP addConfigType.jsp via l'action du formulaire
#MultipartConfig(
fileSizeThreshold = 1024 * 1024 * 1, // 1 MB
maxFileSize = 1024 * 1024 * 10, // 10 MB
maxRequestSize = 1024 * 1024 * 100 // 100 MB
)
public class addHabit extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
HttpSession session = request.getSession(false);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String url="myURL";
Part filePart = request.getPart("monfichier");
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
for (Part part : request.getParts()) {
part.write(url);
}
}
}
I compile using javax.servlet-api-3.0.1
According to this post
How can I upload files to a server using JSP/Servlet?
it should be fine
Can you help me ?

The docs of that method state:
Since:
Servlet 3.1
that means that your javax.servlet-api-3.0.1 is too old. There is a version 3.1.0 available.
I just realized that the question you linked also provides these answers already, as well as a copy-pastable workaround, have you read that question and answer?

Related

reading multiple parquet files in java takes unresonable amount of memory

Reading 20 uncompressed parquet files with total size 3.2GB, takes more then 12GB in RAM, when reading them "concurrently".
"concurrently" means that I need to read the second file before closing the first file, not multithreading.
The data is time series, so my program needs to read all the files up to some time, and then proceed.
I expect Arrow to use the amount of memory that corresponds to a single batch multiplied by the amount of files, but in reality the memory used is much more then the entire files.
The files were created with pandas default config (using pyarrow), and reading them in java gives the correct values.
when reading each file to the fullest, and then closing the file, the amount of ram used is ok.
I have tried to switch between the netty, and unsafe memory jars but they have the same results.
-Darrow.memory.debug.allocator=true did not produce any error.
trying to limit the amount of direct memory (the excess memory is outside of the JVM) I have tried to replace NativeMemoryPool.getDefault() with
NativeMemoryPool.createListenable(DirectReservationListener.instance()) or NativeMemoryPool.createListenable(.. some custom listener ..)
but the result is exception:
Exception in thread "main" java.lang.RuntimeException: JNIEnv was not attached to current thread
at org.apache.arrow.dataset.jni.JniWrapper.nextRecordBatch(Native Method)
at org.apache.arrow.dataset.jni.NativeScanner$NativeReader.loadNextBatch(NativeScanner.java:134)
at ParquetExample.main(ParquetExample.java:47)
using -XX:MaxDirectMemorySize=1g, -Xmx4g anyways had no effect.
the runtime is using env varibale:
_JAVA_OPTIONS="--add-opens=java.base/java.nio=ALL-UNNAMED"
on JDK 17.0.2 with arrow 9.0.0
the code is extracted to this simple example, taken from the official documentation:
import org.apache.arrow.dataset.file.FileFormat;
import org.apache.arrow.dataset.file.FileSystemDatasetFactory;
import org.apache.arrow.dataset.jni.NativeMemoryPool;
import org.apache.arrow.dataset.scanner.ScanOptions;
import org.apache.arrow.dataset.scanner.Scanner;
import org.apache.arrow.dataset.source.Dataset;
import org.apache.arrow.dataset.source.DatasetFactory;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class ParquetExample {
static BufferAllocator allocator = new RootAllocator(128 * 1024 * 1024); // limit does not affect problem
public static ArrowReader read_parquet_file(Path filePath, NativeMemoryPool nativeMemoryPool) {
String uri = "file:" + filePath;
ScanOptions options = new ScanOptions(/*batchSize*/ 64 * 1024 * 1024);
try (
DatasetFactory datasetFactory = new FileSystemDatasetFactory(
allocator, nativeMemoryPool, FileFormat.PARQUET, uri);
Dataset dataset = datasetFactory.finish()
) {
Scanner scanner = dataset.newScan(options);
return scanner.scan().iterator().next().execute();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
List<VectorSchemaRoot> schemaRoots = new ArrayList<>();
for (Path filePath : [...] ) { // 20 files, total uncompressed size 3.2GB
ArrowReader arrowReader = read_parquet_file(file,
NativeMemoryPool.getDefault());
if (arrowReader.loadNextBatch()) { // single batch read
schemaRoots.add(arrowReader.getVectorSchemaRoot());
}
}
}
}
the question is - why Arrow using so much memory in a straight-forward example, and why replacing the NativeMemoryPool results in crash?
Thanks

Error: cannot find symbol class Base64 [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
import org.apache.commons.codec.binary.Base64;
import java.util.Base64;
It gives me an error when I run the code.
private void upload() {
Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeBytes(ba);
// Upload image to server
new uploadToServer().execute();
}
The errors I got are:
Error:(81, 21) error: cannot find symbol method encodeBytes(byte[])
Error:(43, 25) error: cannot find symbol class Base64
You are importing Java library. Use android library for Base64 instead.
import android.util.Base64;

Search and replce a string in 10000 files

EDIT: I have 10,000 identical files with below text in it.
TRANSACTION_ID=9093626660000000001,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00001,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting
My requirement is to replace as below in 10 k files.
TRANSACTION_ID from 9093626660000000001 to 9093626660000010000.
and
VAULT_REPORT_NAME from VUpldr_QA_1Mb_Report00001 to
VUpldr_QA_1Mb_Report10000
So my output files contents would be
file 1st:
TRANSACTION_ID=9093626660000000001,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00001,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting
file 2nd:
TRANSACTION_ID=9093626660000000002,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00002,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting
file 10000th:
TRANSACTION_ID=9093626660000010000,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report10000,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting
I wrote below code but that isn't working:
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class mdScript {
public static void main(String[] args) throws IOException {
for (int i=1;i<=10000;i++){
StringBuffer pathVarBuf = new StringBuffer();
pathVarBuf.append("/Users/564169/Desktop/Vault_Testing/vUpldr/MD/1MbReport");
pathVarBuf.append(i);
pathVarBuf.append(".md");
//System.out.println(pathVarBuf);
Path path = Paths.get(pathVarBuf.toString());
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll("VUpldr_QA_1Mb_Report00001", "RVUpldr_QA_1Mb_Report"+i);
int id=999900001; // (Transaction id in the org MD file is 90)
id = id+i;
content = content.replaceAll("999900001", Integer.toString(id));
Files.write(path, content.getBytes(charset));
System.out.println(content);
}
}
}
I am getting below error:
Exception in thread "main" java.lang.NullPointerException
at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:267)
at java.nio.file.Paths.get(Paths.java:84)
at mdScript.main(mdScript.java:22)
There are many ways to do this. You can do it programmatically as well.
Simple solution will be using TextPad
Here is a link for the same
TextPad find and replace in files

How to Decode a URL using Java in Android? [duplicate]

This question already has answers here:
URL encoding in Android
(7 answers)
Closed 9 years ago.
I am newbie in android, i have this url now i need to encode this url: the url that has to be encoded.
With Reference of downloading apk in Mobile using Java how can i pass this url to this Line :
updateAppInstance.execute("http://demo.ingresssolutions.com/proposalmanagement/services/user/getApkFile");
Previous post Regarding Apk Download
http://demo.ingresssolutions.com/proposalmanagement/services/user/uploadFile
Contrary to many answers you will read, java.net.URLEncoder isn't for encoding URLs. It is for encoding URL and POST arguments.
The correct way to encode a URL in Java is as given in this answer, or, if you only need to encode the path component:
String encodedPath = new URI(null, path, null).toASCIIString();
import java.net.URL;
import java.net.URLEncoder;
import java.net.MalformedURLException;
import java.io.UnsupportedEncodingException;
public class Encoder{
public static void main(String[] args){
URL url = null;
try{
url = new URL("http://www.stackoverflow.com");
}catch(MalformedURLException mue){
System.err.println(mue);
}
System.out.println(url + "
");
try{
String encodedurl = URLEncoder.encode(url.toString(),"UTF-8");
System.out.println(encodedurl);
}catch(UnsupportedEncodingException uee){
System.err.println(uee);
}
}
}
Hope this helps you

How do I scrape website with term acceptance page? [duplicate]

This question already has answers here:
How to code an automated bot that can browse and do operations on a webpage
(6 answers)
Closed 7 years ago.
I am new to writing code and I am trying to write code to scrape a specific website. The issue is that this website has a page to accept the conditions of use and privacy page. This can be seen by the website: http://cpdocket.cp.cuyahogacounty.us/
I need to bypass this page somehow and I have no idea how. I am writing my code in Java, and so far have working code that scrapes the source for any website. This code is:
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.StringBuilder;
import java.io.IOException;
// Scraper class takes an input of a string, and returns the source code of the of the website
public class Scraper {
private static String url; // the input website to be scraped
//constructor
public Scraper(String url) {
this.url = url;
}
//scrapeWebsite runs the method to scrape the input variable. As of now it retuns a string. This string idealy should be saved
//so it is able to be parsed by another method
public static String scrapeWebsite() throws IOException {
URL urlconnect = new URL(url); //creates the url from the variable
URLConnection connection = urlconnect.openConnection(); // connects to the created url
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8")); // annonymous class to stream the website
String inputLine; //creates a new variable of string
StringBuilder a = new StringBuilder(); // creates stringbuilder
//loop appends to the string builder as long as there is information
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
}
Any suggestions on how to go about doing this would be greatly appreciated.
I am rewriting the code based off a ruby code. The code is:
def initializeSession()
## SETUP # POST headers
post_header = Hash.new()
post_header['Host'] = 'cpdocket.cp.cuyahogacounty.us'
post_header['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0'
post_header['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
post_header['Accept-Language'] = 'en-US,en;q=0.5'
post_header['Accept-Encoding'] = 'gzip, deflate'
post_header['X-Requested-With'] = 'XMLHttpRequest'
post_header['X-MicrosoftAjax'] = 'Delta=true'
post_header['Cache-Control'] = 'no-cache'
post_header['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
post_header['Referer'] = 'http://cpdocket.cp.cuyahogacounty.us/Search.aspx' # may have to alter this per request
# post_header['Content-Length'] = '12197'
post_header['Connection'] = 'keep-alive'
post_header['Pragma'] = 'no-cache'
# STEP # set up simulated browser and make first request
#browser = SimBrowser.new()
#logname = 'log.txt'
#s = Scribe.new(logname)
session_cookie = 'ASP.NET_SessionId'
url = 'http://cpdocket.cp.cuyahogacounty.us/'
#browser.http_get(url)
#puts browser.get_body() # debug
puts 'DEBUG: session cookie: ' + #browser.get_cookie_var(session_cookie)
#log.slog('DEBUG: home page response code: expected 200, actual ' + #browser.get_response().code)
# s.flog('### HOME PAGE RESPONSE')
# s.flog(browser.get_body()) # debug
# STEP # send our acceptance of the terms of service
data = {
'ctl00$SheetContentPlaceHolder$btnYes' => 'Yes',
'__EVENTARGUMENT'=>'',
'__EVENTTARGET'=>'',
'__EVENTVALIDATION'=>'/wEWBwKc78CQCQLn3/HqCQLZw/fZCgLipuudAQK42duKDQL33NjnAwKn6+K4CIM3TSmrbrsn2xBRJf2DRwg01Vsbdk+oJV9lhG/in+xD',
'__VIEWSTATE'=>'/wEPDwUKLTI4MzA1ODM0OA9kFgJmD2QWAgIDD2QWDgIDD2QWAgIBD2QWCAIBDxYCHgRUZXh0BQ9BbmRyZWEgRi4gUm9jY29kAgMPFgIfAAUfQ3V5YWhvZ2EgQ291bnR5IENsZXJrIG9mIENvdXJ0c2QCBQ8PFgIeB1Zpc2libGVoZGQCBw8PFgIfAWhkZAIHDw9kFgIeB29uY2xpY2sFGmphdmFzY3JpcHQ6d2luZG93LnByaW50KCk7ZAILDw9kFgIfAgUiamF2YXNjcmlwdDpvbkNsaWNrPXdpbmRvdy5jbG9zZSgpO2QCDw8PZBYCHwIFRmRpc3BsYXlQb3B1cCgnaF9EaXNjbGFpbWVyLmFzcHgnLCdteVdpbmRvdycsMzcwLDIyMCwnbm8nKTtyZXR1cm4gZmFsc2VkAhMPZBYCZg8PFgIeC05hdmlnYXRlVXJsBRMvVE9TLmFzcHg/aXNwcmludD1ZZGQCFQ8PZBYCHwIFRWRpc3BsYXlQb3B1cCgnaF9RdWVzdGlvbnMuYXNweCcsJ215V2luZG93JywzNzAsMzcwLCdubycpO3JldHVybiBmYWxzZWQCFw8WAh8ABQYxLjAuNTRkZEnXSWiVLEPsDmlc7dX4lH/53vU1P1SLMCBNASGt4T3B'
}
#post_header['Referer'] = url
#browser.http_post(url, data, post_header)
#log.slog('DEBUG: accept terms response code: expected 200, actual ' + #browser.get_response().code)
#log.flog('### TOS ACCPTANCE RESPONSE')
# #log.flog(#browser.get_body()) # debug
end
can this be done in Java as well?
If you don't understand how to do this, the best way to learn is to do this manually while watching what happens with FireBug (on Firefox) or the equivalent tools for IE, Chrome or Safari.
You must duplicate in your code whatever happens in the protocol when the user accepts the terms & conditions manually.
You must also be aware that the UI presented to the user may not be sent directly as HTML, it may be constructed dynamically by Javascript that would normally run on the browser. If you are not prepared to fully emulate a browser to the point of maintaining a DOM and executing Javascript, then this may not be possible.

Categories