Error: cannot find symbol class Base64 [duplicate] - java

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;

Related

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

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?

Java: convert PDF to image using ghost4j

I am trying to use the ghost4j library to convert a pdf to image. My code looks like this:
import org.ghost4j.document.*;
import org.ghost4j.renderer.*;
import java.util.ArrayList;
import java.util.List;
.
.
.
PDFDocument document = new PDFDocument();
document.load(new File("M:\\test.pdf"));
SimpleRenderer renderer = new SimpleRenderer();
renderer.setResolution(300);
List<Image> images = renderer.render(document);
but I get the following error:
error: cannot find symbol
List images = renderer.render(document);
^ symbol: class Image location: class ImageRead 1 error
which indicates that compiler cannot find symbol "Image". I'm not sure why?

How to convert .mp4 video to byte array in Java? [duplicate]

This question already has answers here:
File to byte[] in Java
(27 answers)
Closed 6 years ago.
So, I have this .mp4 video file which I would like to convert into bytes and send it to my client.
At client, I'll receive all the bytes over RTP and then construct my own .mp4 file.
Please help me doing this, I'm not posting any code because, I don't know where to start and I'm all new to file handling in java
Thanks
You can use the Apache commons IOUtils.toByteArray method to create a byte array from an InputStream
Example:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
class ConvertToByteArray
{
public static void main(String args[])
{
FileInputStream is = null;
try
{
is = new FileInputStream("file.mp4");
byte [] byteArr = IOUtils.toByteArray(is);
}
catch (IOException ioe)
{}
finally
{
// close things
}
}
}
You can download the jar her at Apache Commons IO

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

"cannot find symbol" despite correct variable name

In the following code snippet I really don't understand why the compiler is issuing
the "cannot find symbol" error message.
public class LU62XnsCvr extends Object
{
// these two variables (among many others) are declared here as "public"
static StringBuffer message_data = new StringBuffer();
static File Mesg_File = new File("C:\\...\\Mesg_File.txt"); // path snipped
public static void SendMesg() // This "method" is "called" from various
// sections of the LU62XnsCvr program
{
if (mesgcount == 1)
{
// On First call Connect the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File);
FileChannel MesgChnl = MesgOut.getChannel();
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128);
}
// Send Message to the Message Log
String mesg_str = message_data.toString(); // convert buffer to a string
MesgWork = mesg_str.getBytes(); // Convert string to byte array
Mesg_Bufr.put( MesgWork, bufroffset, MGbuflen ); // copy MesgWork to buffer
MesgChnl.write( Mesg_Bufr ); // write message buffer out to the file channel
Mesg_Bufr.clear();
message_data.append(" "); // set message_data to 16 blanks
for ( ndx = 0; ndx < MGbuflen; ++ndx )
{
MesgWork[ndx] = 0x20; // clear MesgWork byte area using blank character
}
} // End of Send Message log write sub-routine
The above looks okay to me; BUT I get the following:
src\LU62XnsCvr.java:444: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
Mesg_Bufr.put( MesgWork, bufroffset, MGbuflen );
^
src\LU62XnsCvr.java:445: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
MesgChnl.write( Mesg_Bufr );
^
src\LU62XnsCvr.java:445: cannot find symbol
symbol : variable MesgChnl
location: class APPC_LU62.java.LU62XnsCvr
MesgChnl.write( Mesg_Bufr );
^
src\LU62XnsCvr.java:446: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
Mesg_Bufr.clear();
^
Unless I'm missing something here it appears that Mesg_Bufr is "spelled" correctly.
Why can't the compiler "find" the variable?
You're declaring Mesg_Bufr in the if block so it's only visible in that block.
if (mesgcount == 1)
{
//On First call Connect the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File) ;
FileChannel MesgChnl = MesgOut.getChannel() ;
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128) ;
}
Same goes for the others. I can't tell what you're trying to do (and tbh I don't care) but to make that run correctly you probably have to put all the code inside the if, or, better yet, return if mesg != 1.

Categories