当我使用html表单和php上传文件时,是否也可以将文件传递给java小程序?

当我使用html表单和php上传文件时,是否也可以将文件传递给java小程序?,java,php,html,file-upload,applet,Java,Php,Html,File Upload,Applet,我有一个分析图像的java程序。我想让程序在web浏览器中可用 该程序需要用户提供一个图像文件。对于常规java程序,这不是问题,因为您可以访问用户的文件系统。但是,小程序中并非如此。我的计划是让用户使用html表单和php程序将图像上传到我的服务器。这还有一个额外的好处,那就是存储照片以供在网站上使用 出于安全原因,建议将映像放在webroot之上的目录中。但是,这意味着小程序将无法访问图像。是否有办法将图像发送到服务器和小程序?出于安全原因,我希望避免使用发送webroot之外文件的程序。我

我有一个分析图像的java程序。我想让程序在web浏览器中可用

该程序需要用户提供一个图像文件。对于常规java程序,这不是问题,因为您可以访问用户的文件系统。但是,小程序中并非如此。我的计划是让用户使用html表单和php程序将图像上传到我的服务器。这还有一个额外的好处,那就是存储照片以供在网站上使用

出于安全原因,建议将映像放在webroot之上的目录中。但是,这意味着小程序将无法访问图像。是否有办法将图像发送到服务器和小程序?出于安全原因,我希望避免使用发送webroot之外文件的程序。我还想避免不美观的安全警告。有没有其他方法来完成这个项目

以下是我用于上传的php代码:

<?php

// the current directory
$current_directory = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// the location of the program and uploads the file
$uploadProgram = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'webuploadcode.php';

// the maximum file size in bytes for the html upload form
$maximum_file_size = 300000; 

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <title>Upload form</title>
</head>

<body>
<form id="Upload" action="<?php echo $uploadProgram ?>" enctype="multipart/form-data" method="post">
    <h1>Upload form</h1>
    <p>Logged in as: <?php echo $_SERVER['PHP_AUTH_USER'] ?></p>
    <p><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $maximum_file_size ?>"></p>
    <p>
        <label for="file">Select a file to upload:</label>
        <input id="file" type="file" name="file">
    </p>
    <p>
        <label for="submit">Click to </label>
        <input id="submit" type="submit" name="submit" value="Upload File">
    </p>
</form>
</body>
</html>
<?php  

// the directory to receieve the files
$upload_directory = '../../photouploads/';

// the location of the upload form in case we need it
$upload_form = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self .     'webuploadform.php';

// the location of the status page
$upload_status = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self .     'uploadstatus.php';

// name of the fieldname used for the file in the HTML form
$fieldname = 'file';

$username = $_SERVER['PHP_AUTH_USER'];

// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');

// check the upload form was actually submitted else print form
isset($_POST['submit']) or error('the upload form is needed', $upload_form);

// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0) or error($errors[$_FILES[$fieldname]['error']], $upload_form);

// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name']) or error('not an HTTP upload', $upload_form);

// blacklist php files
$blacklist = array(".php", ".phtml", ".php3", ".php4");

foreach ($blacklist as $item)
{ 
    if(preg_match("/$item\$/i", $_FILES[$fieldname]['tmp_name']))
    { 
        echo "We do not allow uploading PHP files.\n";
        exit;
    }
}

// check the size of the image to confirm that it is an image file 
@getimagesize($_FILES[$fieldname]['tmp_name']) or error('only image uploads are allowed', $upload_form);

// make a unique filename for the uploaded file 
$now = time();
//$_POST['credit']
while(file_exists($uploadFilename = $upload_directory.$now.'-'.$username.'.jpg'))
{ 
    $now++;
}

// move the file to the image folder and allocate it with the new filename
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
    or error('receiving directory insuffiecient permission', $upload_form);

// Redirect the client to the status page
header('Location: ' . $uploadStatus);

// an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
    header("Refresh: $seconds; URL=\"$location\"");
    echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
    '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
    '<html lang="en">'."\n".
    '   <head>'."\n".
    '       <meta http-equiv="content-type" content="text/html; charset=iso-    8859-1">    '."\n\n".
    '       <link rel="stylesheet" type="text/css" href="stylesheet.css">    '."\n\n".
    '   <title>Upload error</title>'."\n\n".
    '   </head>'."\n\n".
    '   <body>'."\n\n".
    '   <div id="Upload">'."\n\n".
    '       <h1>Upload failure</h1>'."\n\n".
    '       <p>An error has occured: '."\n\n".
    '       <span class="red">' . $error . '...</span>'."\n\n".
    '       The upload form is reloading</p>'."\n\n".
    '    </div>'."\n\n".
    '</html>';
    exit;
} // end error handler

?>  

上传表单

我有一个分析图像的java程序。我想让程序在web浏览器中可用

使用从链接启动

应用程序。使用JWS启动可以访问API。JNLPAPI提供的对象甚至允许沙盒应用程序。从本地文件系统打开文件并将文件保存到本地文件系统。这是一本书

当使用沙盒时,弹出文件浏览器的操作将调用如下安全提示(取自链接的演示)


JWS还可以用于在插件2JRE中启动嵌入式小程序


如果由开发人员进行数字签名,并且在最终用户提示时受信任,则受信任的小程序可以访问更常见的
JFileChooser
/
文件



服务器上是否需要映像?如果没有,请节省带宽和麻烦。如果是,则显示applet/JWS应用程序。可以在完成完整性检查(例如字节大小、尺寸等)后上载文件。

一个选项是使用小程序加载文件,然后让小程序将其上载到服务器。
另一个不太理想的选择是使用HTTP post将文件上载到服务器,然后让小程序从服务器下载文件。

您在设计中提到了哪些安全问题?如果需要通过小程序将图像返回给用户,请将其放在可访问的web位置。此外,如果对小程序进行签名,您可以从用户本地文件系统读取图像。安全问题涉及恶意用户上载代码(而不是图像)和入侵网站,以及读取存储在webroot上的密码文件等。显然,上传和读取都必须小心处理。我对这些事情不是特别有经验,所以我很谨慎。谢谢你的评论。这是一个沙盒应用程序,因此据我所知,JNLP将向用户提示该文件。我希望避免这种提示。我确实希望服务器上的图像,但图像分析程序不需要它。我只想一次上传图像并分析它。“JNLP将提示用户该文件。”当然,此时用户/应用程序正在运行。转到弹出JNLP文件浏览器,用户将被问到“此应用程序。希望从本地文件系统加载资源。允许吗?”如果用户调用该对话框,他们到底为什么会在该点拒绝?JWS当然是一个选项,您是对的,用户可能不会在该点拒绝。但是,我希望避免出现提示。他们没有创造出我希望我的网站和程序拥有的那种用户体验。为什么后者不那么理想?两种选择是否具有相同的安全风险?我认为允许在其他人的计算机上运行的应用程序将文件上载到服务器的风险更大。因为后者需要两次文件传输(到服务器和从服务器传输),而前者意味着一次文件传输(从小程序到服务器)。