0

I am creating an application where the user can add profiles of different person including his profile picture. However i want to enable the option when there is NO picture to upload. In that way i want to save a default image that it is in package of my project. image in a package of my project

Here is part of the code that i use to save a picture using FileChooser i might be almost the same i think.

String F = TxtRutaFoto.getText();
FileInputStream fis = null; 
try{
File file = new File(F);   
fis = new FileInputStream(file);
int k = JOptionPane.showConfirmDialog(null, "DESEA GUARDAR LOS DATOS DEL JUGADOR?","PREGUNTA", JOptionPane.YES_NO_OPTION);
if(k == JOptionPane.YES_OPTION){   try (
CallableStatement pstm = conexion.conectar.getConnection().prepareCall("{call INSERTARJUGADOR(?,?,?,?,?,?,?)}")
             ){
pstm.setString(1, txtID.getText());
pstm.setString(2, txt_ape_pat.getText());
pstm.setString(3, txt_ape_mat.getText());
pstm.setString(4, txt_nombre.getText());
pstm.setString(5, txt_correo.getText());
pstm.setString(6, txt_direc.getText());
pstm.setBinaryStream(7, fis, (int) file.length());
ResultSet r = pstm.executeQuery(); 
  • so the question is: how do I load resources from the classpath? see here https://stackoverflow.com/a/333385/3959856 – Jack Flamp Sep 04 '17 at 19:34

2 Answers2

0

You need to get the InputStream of the picture in your jar. You can do that by using the method classLoader#getResource method:

String loc = getClass().getClassLoader().getResource("/path/to/resource.jpg");
InputStream is = new URL(loc).openStream();

In the default-case you can set that stream instead of the FileInputStream. You should check loc for being null in case the file can't be found (sometimes you have to leave the leading slash away.

Lothar
  • 5,323
  • 1
  • 11
  • 27
0

Problem solved i used this and its works. First i create a 'resources' folder in where is my default picture. Then i just i used the next code.

File file = new File(getClass().getResource("/resources/user.jpg").getFile());
FileInputStream fis = new FileInputStream(file);

then i just call my procedure

pstm.setBinaryStream(7, fis);
ResultSet r = pstm.executeQuery();