如何使用http java客户端从数据库中删除文件
我正在尝试从phpMyAdmin数据库中删除一个文件。我用php编写了一个脚本,该脚本与数据库交互,并通过输入文件id号来删除特定文件(通过使用web客户端进行验证)。但是现在我想用http java客户机删除。但它不起作用。如何使用http java客户端删除文件。我下载了apache的外部jar文件: 以下是我使用Jswing删除文件的功能:如何使用http java客户端从数据库中删除文件,java,php,Java,Php,我正在尝试从phpMyAdmin数据库中删除一个文件。我用php编写了一个脚本,该脚本与数据库交互,并通过输入文件id号来删除特定文件(通过使用web客户端进行验证)。但是现在我想用http java客户机删除。但它不起作用。如何使用http java客户端删除文件。我下载了apache的外部jar文件: 以下是我使用Jswing删除文件的功能: public void actionPerformed(ActionEvent e) { // TODO Auto-generated met
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button1)
{
int reply = JOptionPane.showConfirmDialog(null, "Do you want to delete a file", "Delete File", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
//JOptionPane.showMessageDialog(null, "HELLO");
String[] options = new String[]{"id","Filename"};
int response = JOptionPane.showOptionDialog(null, "Message", "Title",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if(response == 0)
{
String userInput = (String)JOptionPane.showInputDialog(
null,
"Enter the file name",
"Input",
JOptionPane.PLAIN_MESSAGE,
null,
null,
null);
CloseableHttpClient httpClient = null;
String phpURL = "http://emysdomain.com/";
JavaHttpClient.deleteFile(httpClient, userInput, phpURL);
repaint();
//String phpUrl is my domain name.
//repaint to refresh the table. I think i'm wrong using repaint to refresh the table
}
这是我的http java客户端删除文件:
public static boolean deleteFile(CloseableHttpClient httpClient, String id, String uploadURL){
HttpPost httppost = null;
HttpEntity resEntity = null;
HttpResponse response = null;
httpClient = HttpClients.createDefault();
try {
httppost = new HttpPost(uploadURL+"delete.php");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addTextBody("id", id);
httppost.setEntity(multipartEntity.build());
response = httpClient.execute(httppost);
resEntity = response.getEntity();
if (resEntity != null)
EntityUtils.consume(resEntity);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode <= HttpStatus.SC_TEMPORARY_REDIRECT
&& statusCode >= HttpStatus.SC_OK) //SC_OK = 200
{
return true;
}
}
catch(Exception e) {
e.printStackTrace();
return false;
}
return false;
}
}
公共静态布尔删除文件(CloseableHttpClient httpClient,字符串id,字符串上传URL){
HttpPost HttpPost=null;
HttpEntity当前性=空;
HttpResponse响应=null;
httpClient=HttpClients.createDefault();
试一试{
httppost=newhttppost(uploadURL+“delete.php”);
MultipartEntityBuilder multipartEntity=MultipartEntityBuilder.create();
multipartEntity.addTextBody(“id”,id);
setEntity(multipartEntity.build());
response=httpClient.execute(httppost);
resEntity=response.getEntity();
if(最近性!=null)
实体效用消耗(最近);
int statusCode=response.getStatusLine().getStatusCode();
如果(statusCode=HttpStatus.SC\u OK)//SC\u OK=200
{
返回true;
}
}
捕获(例外e){
e、 printStackTrace();
返回false;
}
返回false;
}
}
下面是我的php删除文件:
<?php
/**
*
*
*
*
*/
include 'db.php';
$con = mysqli_connect($host, $username, $password, $database);
$file_name = $_POST["id"];
if(isset($_POST["id"])){
$id = $_POST["id"];
$result = mysqli_query($con, "SELECT * FROM Files WHERE idFiles='$id'");
$row = mysqli_fetch_array($result);
$url = $row['url'];
unlink($url);
$query = "DELETE FROM Files WHERE idFiles='$id';";
mysqli_query($con,$query);
}
mysqli_close($con);
//redirect
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'webclient.php';
header("Location: http://$host$uri/$extra");
exit;
首先,您没有删除文件。您正在删除数据库表中的一行,该行恰好命名为Files
。你有没有试着效仿这个例子?为什么不在PHP脚本中添加一些日志来查看您在$\u POST
中收到的内容,并在Java应用程序中查看您收到的状态代码呢。那会帮助我们帮助你更多!EntityUtils
的consume
方法中有什么内容?@jbx感谢您的更正。是的,我想说我正在删除一行。