logo
down
shadow

Send full client side file path to server side using p:fileUpload


Send full client side file path to server side using p:fileUpload

By : Mikhail Smirnov
Date : November 18 2020, 01:01 AM
Hope this helps You're basically looking in the wrong direction for the solution. And, you're looking at JSF/PrimeFaces in a wrong way. JSF is in the context of this question just a HTML code generator.
HTML does not support sending full client side file path to the server side. True, older Internet Explorer versions had the awkward security bug that the full client side file path was sent along the file name. But, this is not mandated by HTML. The sole purpose of is to send the file content from client to server, which you're supposed to read via getInputStream() and save on a fixed location yourself. The filename is here just additional metadata. This is usually never used as-is to save the file in the server, to avoid overwrites by other uploads with coincidentally the same filename. The file name is at most used as prefix of the final file name in the server side, or only remembered in order to be redisplayed in "Save As" during a download. But that's it.
code :
File file = new File(uploadedFile.getFileName());
System.out.println(file.getAbsolutePath());
String realPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
System.out.println(realPath);
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
    File selectedFile = fileChooser.getSelectedFile();
    String selectedFileAbsolutePath = selectedFile.getAbsolutePath();
    // ...
} else {
    // User pressed cancel.
}
String url = "/someServletURL?selectedFileAbsolutePath=" + URLDecoder.decode(selectedFileAbsolutePath, "UTF-8");
URLConnection connection = new URL(getCodeBase(), url).openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + getParameter("sessionId"));
InputStream response = connection.getInputStream();
// ...
@WebServlet("/someServletURL")
public class SomeServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse resposne) throws ServletException, IOException {
        String selectedFileAbsolutePath = request.getParameter("selectedFileAbsolutePath");
        // ...
    }

}
<applet ...>
    <param name="sessionId" value="#{session.id}" />
</applet>


Share : facebook icon twitter icon
GWT - Java - I am trying to pass the path of a file from client side to server side

GWT - Java - I am trying to pass the path of a file from client side to server side


By : user3366192
Date : March 29 2020, 07:55 AM
it fixes the issue The answer to this was provide by a colleague and is to take a different approach. Get the information on the server side, return it to the client side and then save it to a file. It took him 2.5 hours to get it working so I will share the solution with you.
First on the client side create a button to retrieve the information:
code :
//Write the results to a file.          
        //Add an Export button
        Button btnExport = new Button("Export Details");
        btnExport.setWidth("105px");
        btnExport.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                java.sql.Date sqlStartDate = dateBoxStart.getValue() == null ? null : new java.sql.Date(dateBoxStart.getValue().getTime());
                java.sql.Date sqlEndDate = dateBoxEnd.getValue() == null ? null : new java.sql.Date(dateBoxEnd.getValue().getTime());

                AsyncCallback<String> callback = new PrintPackSummaryHandler<String>(PackSummaryView.this);
                rpc.printPackSummary(scoutGroupId, sqlStartDate, sqlEndDate, callback); 

            }
        });
        horizontalDatesPanel.add(btnExport);
String printPackSummary(String groupID, Date startDate, Date endDate);
public String printPackSummary(String groupID, java.sql.Date startDate, java.sql.Date endDate) {
    HttpServletResponse resp = getThreadLocalResponse();

    List<PackSummary> list = getPackSummary(groupID, startDate, endDate);
    StringBuilder fileContent = new StringBuilder();
    for(PackSummary pack : list){
        fileContent.append(pack.getMetric()+","+pack.getMetricTotal()+"\n");
    }
    return URLEncoder.encode(fileContent.toString());
}
public List<PackSummary> getPackSummary(String groupID, java.sql.Date startDate, java.sql.Date endDate) {

    List<PackSummary> packSummaryList = new ArrayList<PackSummary>();
    PreparedStatement ps = null;
    // Create connection/statement variables outside of try block
    Connection c = null;

    String selectQry = ("SELECT 'Bronze Boomerang' as metric, COUNT(at_cub_awards.ca_id) as metricCount " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_name LIKE '%Bronze Boomerang%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Silver Boomerang', COUNT(at_cub_awards.ca_id) " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_name LIKE '%Silver Boomerang%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Gold Boomerang', COUNT(at_cub_awards.ca_id) " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_name LIKE '%Gold Boomerang%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Grey Wolf', COUNT(at_cub_awards.ca_id) " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_name LIKE '%Grey Wolf%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Level 1', COUNT(at_cub_awards.ca_id) " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_name LIKE '%Level 1%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Level 2', COUNT(at_cub_awards.ca_id) " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_name LIKE '%Level 2%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Special', COUNT(at_cub_awards.ca_id) " +
            "FROM at_cub_details, at_cub_awards, at_award " + 
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_cub_awards.cd_id " +
            "  AND at_cub_awards.ca_awarded_date >= ? " +
            "  AND at_cub_awards.ca_awarded_date <= ? " +
            "  AND at_cub_awards.aw_id = at_award.aw_id " +
            "  AND at_award.aw_award_type LIKE '%Special Interest%' " +
            "GROUP BY at_award.aw_award_type " +
            "UNION " +
            "SELECT 'Joined', COUNT(at_cub_details.cd_id) " +
            "FROM at_cub_details, at_section_details " +
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_section_details.cd_id " +
            "  AND at_section_details.sd_start_date >= ? " +
            "  AND at_section_details.sd_start_date <= ? " +
            "GROUP BY at_cub_details.grp_id " +
            "UNION " +
            "SELECT 'Left', COUNT(at_cub_details.cd_id) " +
            "FROM at_cub_details, at_section_details " +
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_id = at_section_details.cd_id " +
            "  AND at_section_details.sd_end_date >= ? " +
            "  AND at_section_details.sd_end_date <= ? " +
            "GROUP BY at_cub_details.grp_id " +
            "UNION " +
            "SELECT 'Current', COUNT(at_cub_details.cd_id) " +
            "FROM at_cub_details " +
            "WHERE at_cub_details.grp_id = ? " +
            "  AND at_cub_details.cd_archived IS NULL " +
            "GROUP BY at_cub_details.grp_id;");

    try {
        // Get Connection and Statement from DataSource
        c = ds.getConnection();
        ps = c.prepareStatement(selectQry);

        try {
            // Create a statement and execute the query on it               
            ps.setString(1, groupID);
            ps.setDate(2, (java.sql.Date) startDate);
            ps.setDate(3, (java.sql.Date) endDate);
            ps.setString(4, groupID);
            ps.setDate(5, (java.sql.Date) startDate);
            ps.setDate(6, (java.sql.Date) endDate);
            ps.setString(7, groupID);
            ps.setDate(8, (java.sql.Date) startDate);
            ps.setDate(9, (java.sql.Date) endDate);
            ps.setString(10, groupID);
            ps.setDate(11, (java.sql.Date) startDate);
            ps.setDate(12, (java.sql.Date) endDate);
            ps.setString(13, groupID);
            ps.setDate(14, (java.sql.Date) startDate);
            ps.setDate(15, (java.sql.Date) endDate);
            ps.setString(16, groupID);
            ps.setDate(17, (java.sql.Date) startDate);
            ps.setDate(18, (java.sql.Date) endDate);
            ps.setString(19, groupID);
            ps.setDate(20, (java.sql.Date) startDate);
            ps.setDate(21, (java.sql.Date) endDate);
            ps.setString(22, groupID);
            ps.setDate(23, (java.sql.Date) startDate);
            ps.setDate(24, (java.sql.Date) endDate);
            ps.setString(25, groupID);
            ps.setDate(26, (java.sql.Date) startDate);
            ps.setDate(27, (java.sql.Date) endDate);
            ps.setString(28, groupID);

            //Get result set
            ResultSet result = ps.executeQuery();

            while (result.next()) {
                PackSummary packSummary = new PackSummary(result.getString("metric"), result.getInt("metricCount"));
                packSummaryList.add(packSummary);
            }

            // Clean up
            ps.close();
            c.close();

        } catch (SQLException se) {
            System.out.println("SQLException in getPackSummary: " + se.toString());
        } catch (Exception e) {
            System.out.println("Errors occurred in getPackSummary: " + e.toString());
        }

    } catch (SQLException e1) {
        System.out.println("SQLException in getPackSummary: " + e1.toString());
        e1.printStackTrace();

    } finally {

        // Ensure connection is closed and returned to the pool, even if errors occur.
        // This is *very* important if using a connection pool, because after all the
        // connections are used, the application will hang on getConnection(), waiting
        // for a connection to become available.
        // Any errors from the following closes are just ignored.  The main thing is
        // that we have definitely closed the connection.
        try { if(ps != null) ps.close(); } catch (Exception e) {}
        try { if(c != null) c.close(); } catch (Exception e) {}
    }
    // Done
    return packSummaryList;

}
class PrintPackSummaryHandler<T> implements AsyncCallback<String> {
    //Get the Pack Summary details
    PackSummaryView view;

    public PrintPackSummaryHandler(PackSummaryView view) {
        this.view = view;
    }

    public void onFailure(Throwable ex) {
        System.out.println("RPC call failed - PackSummaryView - PrintPackSummaryHandler.");
        Window.alert("Connection failed - please retry.");
    }
    public void onSuccess(String result) {
        Window.open("data:application/csv;charset=utf-8,"+result,"_parent", "location=no") ;
        System.out.println("Download succeed !!"+result);
    }
}
Time lag between date send in object from client side [GWT] to server side [Spring ]

Time lag between date send in object from client side [GWT] to server side [Spring ]


By : vonnagy
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If you don't specify a time zone, Java will use system time zone when printing dates. Your client and your server have different time zones.
Upload .json file in React(client side) and send it to Node(server side)

Upload .json file in React(client side) and send it to Node(server side)


By : himanshu1191
Date : March 29 2020, 07:55 AM
I wish this help you This is not related to ReactJS; you need to upload a file using fetch or AJAX so you need FormData.
code :
let formData = new FormData()
formData.append('file', fileUpload)
fetch(url, {
  method: 'post',
  headers: {
     // maybe you need headers, it depends from server implementation
  },
  body: formData
});
Using jqueries validation plugin, how to send an error from the server side to the client side?

Using jqueries validation plugin, how to send an error from the server side to the client side?


By : user3789952
Date : March 29 2020, 07:55 AM
wish helps you Yes, there is an example of this.
You basically rules: {FIELD: {remote: "URL"}}, then on validation a call is made to URL?FIELD=value and on the server you return true or false.
How to send a value from client side to server side for server side processing

How to send a value from client side to server side for server side processing


By : Jian Chen
Date : March 29 2020, 07:55 AM
wish help you to fix your issue There is a very simple answer to this. After searching for hours thru dozens of questions posted along the same lines and many people offering overly complicated ajax post back solutions, I came up with this. Basically a one liner. Hope it helps someone:
In your javascript you just call the method:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org