apache2.2+perl/cgi的正确配置

apache2.2+perl/cgi的正确配置
2007年10月30日 19:33

为了学习perl语言,就配置环境,但老是出问题,因为是初学者,就在网上到处找方案,最后弄的乱七八糟的,最后经过阅读相关文档终于配置成功,现在把配置情况记录下来,给哪些学习perl语言的朋友。

1。确认你的apche目录里有这个模块:mod_perl.so

2。然后配置httpd.conf

      【1】添加内容: LoadModule perl_module modules/mod_perl.so;

          【2】 确保 文件里存在:ScriptAlias /cgi-bin/ "C:/Inetpub/wwwroot/cgi-bin/"

其中"C:/Inetpub/wwwroot/cgi-bin/" 是我服务器目录下的一个存放.pl文件的, 一定要保证cgi-bin文件夹为可读可访问的。我这个是在c:盘所有终找不到文件安全无法修改,最后才发现是修改它为共享,设置权限为可读可写既可了。

    【3】确保文件里有以下内容。

      把内容

<Directory "C:/Apache/cgi-bin"> 
AllowOverride None 
Options None 
</Directory>

     改为:

       <Directory "C:/Inetpub/wwwroot/cgi-bin">
       AllowOverride All 
       Options All    
       Order allow,deny
       Allow from all
      </Directory>

     AddHandler cgi-script .cgi .pl

说明:

将 Options Indexes FollowSymLinks MultiViews 改为:Options All 
将 AllowOverride None 改为: AllowOverride All

讲AddHandler cgi-script .cgi 给为AddHandler cgi-script .cgi .pl

"C:/Inetpub/wwwroot/cgi-bin/" 是我服务器目录下的一个存放.pl文件的,这个要和上面设置的存放目录要一样。

这样基本上可以了,你可以测试一下。

在服务器目录的cgi-bin下,写一个PERL文件,保存文件名为test.pl: 
如: 
#!C:/Perl/ bin/perl.exe 
print "Content-Type: text/html"; 
print "It works ok!";

打开IE或NETSCAPE浏览器,在URL地址栏中键入:http://localhost/test.pl,可以看到浏览

器上出现 
It works ok! 





一、Windows   下的安装     
        当然,你首先得要有Apache的安装程序。不妨先到http://www.apache.org/dist看看。在这里有世界范围的Apache镜像站 点列表,找到离你最近的那个。如果在国内,这里建议你到软件屋去看看。Apache的安装程序一般的文件名是 apache_1_3_x.win32.exe。把它当到你机器里,运行它。     
        安装程序会提示你修改Apache安装的缺省目录位置C:/Program   Files/Apache   Group/Apache。为了方便以后的配置和维护,建议你安装在根目录下,比方C:/Apache。之后,你会被提示选择安装类型,建议你选择 Custom(定制),这样,你将知道安装了些什么。具体项目有Application   File(应用程序文件)、Source   Code(源代码)、Apache   Manual(Apache   手册)、Additional   Modules(附加模块)、Icons(图标)、Repuired   Directories(必须目录)。你可以根据你的情况来决定如何安装。     
        一切顺利的话(此时不应该有什么问题了),安装很快就会完成。Apache会自动的完成一些基本配置,你可以用UltraEdit-32(或者是其他的文本编辑软件)打开conf/httpd.conf文件大致了解一下。         
        二、配置   Apache   Server     
        这里首先进行的是基本的配置,以便你的Apache   Server以你的意愿运行起来。     
        用UltraEdit-32(或者是其他的文本编辑软件)打开conf/httpd.conf文件。     
        1、寻找到ServerName。这里定义你的域名。这样,当Apache   Server运行时,你可以在浏览器中访问自己的站点。如果前面有#,记得删除它,这是conf的注释标记。     
        2、寻找到ServerAdmin。这里输入你的E-Mail地址。如果你仅仅是单机使用,改不改没什么关系。     
        3、寻找到<Directory   />。向下有一句Options,去掉后面所有的参数,加一个All(注意区分大小写!下同。);接着还有一句Allow   Override,也同样去掉后面所有的参数,加一个All。     
        4、寻找到DocumentRoot。这个语句指定你的网站路径,也就是你主页放置的目录。你可以使用默认的,也可以自己指定一个,但记住,这句末尾不要 加“/”。此外注意,目录的“/”在Apache   Server里写成“/”。     
        5、寻找到DirectoryIndex。这就是你站点第一个显示的主页。     
    
        更改完这些,就可以启动Apache   Server了。是不是看到自己的主页了呢?如果不行,按照上面的步骤再仔细修改。         
        三、增加对   Perl   CGI   脚本的支持     
        要使自己的Apache   Server支持CGI脚本,首先也要安装Perl   for   Win32。安装的过程就不详细的讲述了。这里假设Perl安装在C:/Perl目录下。打开httpd.conf文件。     
        1、寻找到ScriptAlias   /cgi-bin/   "C:/Apache/cgi-bin/"。删除前面的#,将""中的路径指向你存放CGI脚本的目录,也就是,这个目录中的CGI脚本可以被执行。     
        2、寻找到<Directory   "C:/Apache/cgi-bin">。将""中的路径指向你存放CGI脚本的目录,要与上面的相同。更改Options、Allow   Override的参数为All。     
        3、寻找到AddHandler   cgi-script   .cgi。删除前面的#,在后面加上   .pl。     
    
        注意:   CGI脚本中的Perl解释路径改为:#!C:/perl/bin/perl。否则,会出现500错误。     
        四、增加对   PHP   脚本的支持     
        同样,这里也需要安装   PHP   的   Windows   版本。假设安装在C:/php目录下。     
        在httpd.conf文件中加上:     
      ScriptAlias   /php/   "c:/php/"     
      AddType   application/x-httpd-php3   .php3   .php   .phtml     
      Action   application/x-httpd-php3   "/php/php.exe"     
        你的PHP脚本可以放在站点的任意位置(除了cgi-bin)。     
    
        进行到这里,你的   Apache   Server   可以同时支持   CGI   和   PHP   了。






1 首先修改apache2的配置文件,打开/etc/apache2/apache2.conf,找到
#AddHandler cgi-script .cgi 
一行,并改为
AddHandler cig-script .cgi .pl
2 安装libapache2-mod-perl2 软件包
3 安装好后可以对CGI脚本路径进行虚拟设置,如下面的设置:
ScriptAlias /cgi/ /home/shappen/perl/cgi-bin/ 

Options +ExecCGI 
AddHandler cgi-script .cgi .pl 


这样在浏览器中访问根目录下的cgi目录,便会重定向到/home/shappen/perl/cgi-bin/目录
4 至此安装成功,可以在cgi目录下放置测试文件进行测试(注意两个换行符):
cat test.pl
#!/usr/bin/perl 
print "Content-type: text/html\n\n"; 
print "This is a test!";

chmod a+x test.pl
5 重启apache2 /etc/init.d/apache2 restart






首先安装sudo apt-get install libperl-dev

http://httpd.apache.org/download.cgi#apache22下载httpd

安装方法:./configure  make  make install

这样你的apache2就安装到了/usr/local/apache2下.

cd  /usr/local/apache2/bin

sudo ./apachectl start

在地址栏输入127.0.0.1,发现“It works!",页面的内家取决于htdocs/index.html的内容。

任何地方都可以调用apachectl

sudo vim /root/.bashrc

export PATH="$PATH:/usr/local/apache2/bin"

vim ~/.bashrc

export PATH="$PATH:/usr/local/apache2/bin"

设置开机自动启动Apache

在/etc/init.d下建立一个shell文件apache

#!/bin/bash
/usr/local/apache2/bin/apachectl start

$chmod +x apache

在/etc/rc5.d下建立一个指向apache的软链接

$ln -s /etc/init.d/apache S20apache

因为rc(0--6,S).d下的文件是在系统启动后自动执行的嘛,另外rc(0--6,S).d下文件名的命名规则:以S开头是启动,以K开关不是启动。注意rc.d下建立的软件链接指向的文件必须是/etc/init.d下的文件。

最后执行update-rc.d apache defaults

详细内容参加rc5.d下面的README文件。

实际上开机自动启动还有另外一种简单的方式:System==>Preference==>Startup Applications

在/usr/local/apache2/cgi-bin下已经给了个测试程序test-cgi,我们在地址栏是输入127.0.0.1/cgi-bin/test-cgi发现“Internal Server Error",出现这个问题有三种原因:

  1. 找不到脚本解释器。shell的话第一行就用#!/bin/sh,Perl的话第一行就用#!/usr/bin/perl
  2. 脚本语法有误
  3. 权限问题。从浏览器中访问apache服务器文件的用户默认组别是"Nobody",执行sudo chmod 755 test-cgi就解决问题了。

sudo chmod 777 /usr/local/apache2/cgi-bin

在cgi-bin下新建一个文件hello.pl

#!/usr/bin/perl
print "Content-type: text/html";
print "Hello, World.";

$chmod 755 hello.pl

在浏览器地址栏输入http://localhost/cgi-bin/hello.pl测验一下。

下面给一个正式的CGI代码,也是我做的第一个搜索引擎的demo,具有划时代的意义!

#!/usr/bin/perl
use CGI qw(:standard);
use Search::Xapian;
print header;
print start_html(-title=>'ISee Search Engine',-align=>'center');
print start_form,
    textfield('terms'),
    submit('Search'),
    p,
    end_form;
print hr;
if(param()){
    my $db = Search::Xapian::Database->new("/home/orisun/master/db2");
    my $term=param('terms');
    my $enq $db->enquire($term);
    ##printf "Running query '%s'\n", $enq->get_query()->get_description();
    my @matches $enq->matches(0, 10);
    print scalar(@matches) . " results found";
    print br;
    foreach my $match @matches ) {
        my $doc $match->get_document();
        printf "ID=%d Percent=%d%%<br>%s"$match->get_docid(), $match->get_percent(), $doc->get_data();
        print p;
    }
}
print end_html;

运行效果:

彻底删除Apache
首先停止服务:
service httpd stop
apchectl stop
再删除
1、sudo apt-get  --purge remove apache2
2、sudo apt-get --purge remove apache2.2-common
3、sudo apt-get autoremove (此命令会自动卸载PHP)
最后手动删除Apache的所有文件









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Practical Mod Perl<br><br> Copyright <br> Preface <br> What You Need to Know <br> Who This Book Is For <br> How This Book Is Organized <br> Reference Sections <br> Filesystem Conventions <br> Apache and Perl Versions <br> Typographic Conventions <br> Command Interpreter Program (Shell) Conventions <br> Installing Perl Modules <br> How to Contact Us <br> Acknowledgments <br> <br> Part I: mod_perl Administration <br> Chapter 1. Introducing CGI and mod_perl <br> Section 1.1. A Brief History of CGI <br> Section 1.2. The Apache 1.3 Server Model <br> Section 1.3. The Development of mod_perl 1.0 <br> Section 1.4. Apache 1.3 Request Processing Phases <br> Section 1.5. References <br> <br> Chapter 2. Getting Started Fast <br> Section 2.1. Installing mod_perl 1.0 in Three Steps <br> Section 2.2. Installing mod_perl on Unix Platforms <br> Section 2.3. Configuring and Starting the mod_perl Server <br> Section 2.4. Installing mod_perl for Windows <br> Section 2.5. Preparing the Scripts Directory <br> Section 2.6. A Sample Apache::Registry Script <br> Section 2.7. A Simple mod_perl Content Handler <br> Section 2.8. Is This All We Need to Know About mod_perl? <br> Section 2.9. References <br> <br> Chapter 3. Installing mod_perl <br> Section 3.1. Configuring the Source <br> Section 3.2. Building mod_perl (make) <br> Section 3.3. Testing the Server (make test) <br> Section 3.4. Installation (make install) <br> Section 3.5. Installation Scenarios for Standalone mod_perl <br> Section 3.6. Building mod_perl with Other Components <br> Section 3.7. Installing mod_perl with the CPAN.pm Interactive Shell <br> Section 3.8. Installing mod_perl on Multiple Machines <br> Section 3.9. Installation into a Nonstandard Directory <br> Section 3.10. How Can I Tell if mod_perl Is Running? <br> Section 3.11. General Notes <br> Section 3.12. References <br> <br> Chapter 4. mod_perl Configuration <br> Section 4.1. Apache Configuration <br> Section 4.2. mod_perl Configuration <br> Section 4.3. The Startup File <br> Section 4.4. Apache Configuration in Perl <br> Section 4.5. Validating the Configuration Syntax <br> Section 4.6. The Scope of mod_perl Configuration Directives <br> Section 4.7. Apache Restarts Twice <br> Section 4.8. Enabling Remote Server Configuration Reports <br> Section 4.9. Tips and Tricks <br> Section 4.10. Configuration Security Concerns <br> Section 4.11. General Pitfalls <br> Section 4.12. References <br> <br> Chapter 5. Web Server Control, Monitoring, Upgrade, and Maintenance <br> Section 5.1. Starting the Server in Multi-Process Mode <br> Section 5.2. Starting the Server in Single-Process Mode <br> Section 5.3. Using kill to Control Processes <br> Section 5.4. Using apachectl to Control the Server <br> Section 5.5. Validating Server Configuration <br> Section 5.6. Setuid root Startup Scripts <br> Section 5.7. Preparing for Machine Reboot <br> Section 5.8. Upgrading a Live Server <br> Section 5.9. Three-Tier Server Scheme: Development, Staging, and Production <br> Section 5.10. Web Server Monitoring <br> Section 5.11. Server Maintenance Chores <br> Section 5.12. References <br> <br> Chapter 6. Coding with mod_perl in Mind <br> Section 6.1. Before You Start to Code <br> Section 6.2. Exposing Apache::Registry Secrets <br> Section 6.3. Namespace Issues <br> Section 6.4. Perl Specifics in the mod_perl Environment <br> Section 6.5. CHECK and INIT Blocks <br> Section 6.6. Apache::Registry Specifics <br> Section 6.7. Transition from mod_cgi Scripts to Apache Handlers <br> Section 6.8. Loading and Reloading Modules <br> Section 6.9. Handling the "User Pressed Stop Button" Case <br> Section 6.10. Handling Server Timeout Cases and Working with $SIG{ALRM} <br> Section 6.11. Generating Correct HTTP Headers <br> Section 6.12. Method Handlers: The Browse and See, Browse and View Example <br> Section 6.13. References <br> <br> <br> Part II: mod_perl Performance <br> Chapter 7. Identifying Your Performance Problems <br> Section 7.1. Looking at the Big Picture <br> Section 7.2. Asking the Right Questions <br> Section 7.3. References <br> <br> Chapter 8. Choosing a Platform for the Best Performance <br> Section 8.1. Choosing the Right Operating System <br> Section 8.2. Choosing the Right Hardware <br> Section 8.3. References <br> <br> Chapter 9. Essential Tools for Performance Tuning <br> Section 9.1. Server Benchmarking <br> Section 9.2. Perl Code Benchmarking <br> Section 9.3. Process Memory Measurements <br> Section 9.4. Apache::Status and Measuring Code Memory Usage <br> Section 9.5. Code Profiling Techniques <br> Section 9.6. References <br> <br> Chapter 10. Improving Performance with Shared Memory and Proper Forking <br> Section 10.1. Sharing Memory <br> Section 10.2. Forking and Executing Subprocessesfrom mod_perl <br> Section 10.3. References <br> <br> Chapter 11. Tuning Performance by Tweaking Apache's Configuration <br> Section 11.1. Setting the MaxClients Directive <br> Section 11.2. Setting the MaxRequestsPerChild Directive <br> Section 11.3. Setting MinSpareServers, MaxSpareServers, and StartServers <br> Section 11.4. KeepAlive <br> Section 11.5. PerlSetupEnv <br> Section 11.6. Reducing the Number of stat( ) Calls Made by Apache <br> Section 11.7. Symbolic Links Lookup <br> Section 11.8. Disabling DNS Resolution <br> Section 11.9. Response Compressing <br> Section 11.10. References <br> <br> Chapter 12. Server Setup Strategies <br> Section 12.1. mod_perl Deployment Overview <br> Section 12.2. Standalone mod_perl-Enabled Apache Server <br> Section 12.3. One Plain and One mod_perl-Enabled Apache Server <br> Section 12.4. One Light Non-Apache and One mod_perl-Enabled Apache Server <br> Section 12.5. Adding a Proxy Server in httpd Accelerator Mode <br> Section 12.6. The Squid Server and mod_perl <br> Section 12.7. Apache's mod_proxy Module <br> Section 12.8. mod_rewrite Examples <br> Section 12.9. Getting the Remote Server IP in the Backend Server in the Proxy Setup <br> Section 12.10. Frontend/Backend Proxying with Virtual Hosts <br> Section 12.11. HTTP Authentication with Two Servers and a Proxy <br> Section 12.12. When One Machine Is Not Enough for Your RDBMS DataBase and mod_perl <br> Section 12.13. Running More than One mod_perl Server on the Same Machine <br> Section 12.14. SSL Functionality and a mod_perl Server <br> Section 12.15. Uploading and Downloading Big Files <br> Section 12.16. References <br> <br> Chapter 13. TMTOWTDI: Convenience and Habit Versus Performance <br> Section 13.1. Apache::Registry PerlHandler Versus Custom PerlHandler <br> Section 13.2. Apache::args Versus Apache::Request::param Versus CGI::param <br> Section 13.3. Buffered Printing and Better print( ) Techniques <br> Section 13.4. Interpolation, Concatenation, or List <br> Section 13.5. Keeping a Small Memory Footprint <br> Section 13.6. Object Methods Calls Versus Function Calls <br> Section 13.7. Using the Perl stat( ) Call's Cached Results <br> Section 13.8. time( ) System Call Versus $r->request_time <br> Section 13.9. Printing Unmodified Files <br> Section 13.10. Caching and Pre-Caching <br> Section 13.11. Caching with Memoize <br> Section 13.12. Comparing Runtime Performance of Perl and C <br> Section 13.13. References <br> <br> Chapter 14. Defensive Measures for Performance Enhancement <br> Section 14.1. Controlling Your Memory Usage <br> Section 14.2. Coding for a Smaller Memory Footprint <br> Section 14.3. Conclusion <br> Section 14.4. References <br> <br> Chapter 15. Improving Performance Through Build Options <br> Section 15.1. Server Size as a Function of Compiled-in Features <br> Section 15.2. mod_status and ExtendedStatus On <br> Section 15.3. DYNAMIC_MODULE_LIMIT Apache Build Option <br> Section 15.4. Perl Build Options <br> Section 15.5. Architecture-Specific Compile Options <br> Section 15.6. References <br> <br> Chapter 16. HTTP Headers for Optimal Performance <br> Section 16.1. Date-Related Headers <br> Section 16.2. Content Headers <br> Section 16.3. Content Negotiation <br> Section 16.4. HTTP Requests <br> Section 16.5. Avoiding Dealing with Headers <br> Section 16.6. References <br> <br> <br> Part III: Databases and mod_perl <br> Chapter 17. Databases Overview <br> Section 17.1. Volatile Databases <br> Section 17.2. Non-Volatile Databases <br> Section 17.3. References <br> <br> Chapter 18. mod_perl Data-Sharing Techniques <br> Section 18.1. Sharing the Read-Only Data in and Between Processes <br> Section 18.2. Sharing Data Between Various Handlers <br> Section 18.3. References <br> <br> Chapter 19. DBM and mod_perl <br> Section 19.1. mod_perl and DBM <br> Section 19.2. Resource Locking <br> Section 19.3. Flawed Locking Methods <br> Section 19.4. Locking Wrappers Overview <br> Section 19.5. Tie::DB_Lock <br> Section 19.6. Examples <br> Section 19.7. References <br> <br> Chapter 20. Relational Databases and mod_perl <br> Section 20.1. Persistent Database Connections with Apache::DBI <br> Section 20.2. Improving Performance <br> Section 20.3. DBI Debug Techniques <br> Section 20.4. References <br> <br> <br> Part IV: Debugging and Troubleshooting <br> Chapter 21. Error Handling and Debugging <br> Section 21.1. Warnings and Errors Explained <br> Section 21.2. Debugging Code in Single-Server Mode <br> Section 21.3. Tracing System Calls <br> Section 21.4. Tracing mod_perl-Specific Perl Calls <br> Section 21.5. Debugging Perl Code <br> Section 21.6. Analyzing Dumped core Files <br> Section 21.7. Hanging Processes: Detection and Diagnostics <br> Section 21.8. Useful Debug Modules <br> Section 21.9. Looking Inside the Server <br> Section 21.10. References <br> <br> Chapter 22. Troubleshooting mod_perl <br> Section 22.1. Configuration and Startup <br> Section 22.2. Code Parsing and Compilation <br> Section 22.3. Runtime <br> Section 22.4. Shutdown and Restart <br> <br> Chapter 23. Getting Help and Online Resources <br> Section 23.1. How to Report Problems <br> Section 23.2. Mailing List Etiquette <br> Section 23.3. Resources <br> <br> <br> Part V: mod_perl 2.0 <br> Chapter 24. mod_perl 2.0: Installation and Configuration <br> Section 24.1. What's New in Apache 2.0 <br> Section 24.2. What's New in Perl 5.6.0-5.8.0 <br> Section 24.3. What's New in mod_perl 2.0 <br> Section 24.4. Installing mod_perl 2.0 <br> Section 24.5. Configuring mod_perl 2.0 <br> Section 24.6. Resources <br> <br> Chapter 25. Programming for mod_perl 2.0 <br> Section 25.1. Migrating to and Programming with mod_perl 2.0 <br> Section 25.2. New Apache Phases and Corresponding Perl*Handlers <br> Section 25.3. I/O Filtering <br> <br> <br> Part VI: Appendixes <br> Appendix A. mod_perl Recipes <br> Section A.1. Emulating the Authentication Mechanism <br> Section A.2. Reusing Data from POST Requests <br> Section A.3. Redirecting POST Requests <br> Section A.4. Redirecting While Maintaining Environment Variables <br> Section A.5. Handling Cookies <br> Section A.6. Sending Multiple Cookies with the mod_perl API <br> Section A.7. Sending Cookies in REDIRECT Responses <br> Section A.8. CGI::params in the mod_perlish Way <br> Section A.9. Sending Email from mod_perl <br> Section A.10. mod_rewrite in Perl <br> Section A.11. Setting PerlHandler Based on MIME Type <br> Section A.12. Singleton Database Handles <br> Section A.13. Terminating a Child Process on Request Completion <br> Section A.14. References <br> <br> Appendix B. Apache Perl Modules <br> Section B.1. Development-Stage Modules <br> Section B.2. Modules to Aid Debugging <br> Section B.3. Control and Monitoring Modules <br> Section B.4. Server Configuration Modules <br> Section B.5. Authentication-Phase Modules <br> Section B.6. Authorization-Phase Modules <br> Section B.7. Access-Phase Modules <br> Section B.8. Type Handlers <br> Section B.9. Trans Handlers <br> Section B.10. Fixup Handlers <br> Section B.11. Generic Content-Generation Modules <br> Section B.12. Application-Specific Content-Generation Modules <br> Section B.13. Database Modules <br> Section B.14. Toolkits and Frameworks for Content-Generation and Other Phases <br> Section B.15. Output Filters and Layering Modules <br> Section B.16. Logging-Phase Handlers <br> Section B.17. Core Apache Modules <br> Section B.18. Other Miscellaneous Modules <br> <br> Appendix C. ISPs Providing mod_perl Services <br> Section C.1. Users Sharing a Single Web Server <br> Section C.2. Users Sharing a Single Machine <br> Section C.3. Giving Each User a Separate Machine (Colocation) <br> Section C.4. Giving Each User a Virtual Machine <br> <br> Appendix D. The Template Toolkit <br> Section D.1. Fetching and Installing the Template Toolkit <br> Section D.2. Overview <br> Section D.3. Typical Uses <br> Section D.4. Template Toolkit Language <br> Section D.5. Processing Templates <br> Section D.6. Apache/mod_perl Handler <br> Section D.7. Apache::Template Module <br> Section D.8. Hangman Application <br> Section D.9. References <br> <br> Appendix E. The AxKit XML Application Server <br> Section E.1. Installing and Configuring AxKit <br> Section E.2. Your First AxKit Page <br> Section E.3. Dynamic Content <br> Section E.4. More XPathScript Details <br> Section E.5. XSLT <br> Section E.6. Putting Everything Together <br> Section E.7. More Reasons to Use AxKit <br> <br> Appendix F. HTTP Status Codes <br> Section F.1. HTTP/1.0 Status Codes <br> Section F.2. HTTP/1.1 Status Codes <br> Section F.3. References <br> <br> <br> Colophon <br> Index <br>
根据提供的引用内容,您遇到的问题是关于文件上传和表单数据传递的。具体来说,您的请求没有包含正确的`multipart/form-data`或`multipart/mixed`流,导致内容类型头部为null。 为了解决这个问题,您可以使用`commons-fileupload`插件来处理文件上传,并确保在表单中正确设置`enctype`为`multipart/form-data`。此外,您还可以使用`commons-io`插件来处理项目全局编码问题。 以下是一个示例代码,演示了如何使用`commons-fileupload`插件来处理文件上传和表单数据传递: ```java import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.List; public class FileUploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { // 处理普通表单字段 String fieldName = item.getFieldName(); String fieldValue = item.getString(); // TODO: 处理表单字段的值 } else { // 处理文件上传字段 String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); long sizeInBytes = item.getSize(); // TODO: 处理文件上传的内容 } } // TODO: 处理完文件上传和表单数据传递后的逻辑 } catch (Exception e) { e.printStackTrace(); // TODO: 处理异常情况 } } else { // TODO: 处理非文件上传请求 } } } ``` 请注意,上述示例代码是基于Java Servlet的,您需要将其集成到您的项目中,并根据您的具体需求进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值