求上传和下载的JSP源代码,高分相送,最好没有bean的

rain_ok 2002-05-27 08:21:43
急!
...全文
61 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaolihhy 2002-08-31
  • 打赏
  • 举报
回复
还是错误!!
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: upload failed.
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471)
at org.apache.jsp.fileUpLoadOp$jsp._jspService(fileUpLoadOp$jsp.java:94)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2349)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:646)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:469)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:644)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:483)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:405)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:376)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:508)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:518)
at java.lang.Thread.run(Thread.java:536)

zhaolihhy 2002-08-31
  • 打赏
  • 举报
回复
为什么不说出来????我还是迷糊,求你了
rain_ok 2002-05-27
  • 打赏
  • 举报
回复
我是大迷糊,我知道是咋回事了,^_^
rain_ok 2002-05-27
  • 打赏
  • 举报
回复
呜,我被欺骗了,搞不定阿,我将程序改为
import java.io.*;
public class fileLoad {
public fileLoad() {
}
public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
try {
String contentType=req.getContentType();
int contentLength=req.getContentLength();
java.io.DataInputStream bis=new java.io.DataInputStream(req.getInputStream());
int once = 0;
int total = 0;
byte[] buffer=new byte[contentLength];
while ((total<contentLength) && (once>=0)) {
once = bis.read(buffer,total,contentLength);
total += once;
}
int boundaryStart=contentType.indexOf("boundary=");
boundaryStart=boundaryStart+"boundary=".length();
String boundary="--"+contentType.substring(boundaryStart);
int pos=getFormNameIndex(buffer,"filename=\"".getBytes(),1)+"filename=\"".length();
int posEnd=getFormNameIndex(buffer,"\"".getBytes(),pos);
String filename=new String(buffer,pos,posEnd-pos);
pos=filename.lastIndexOf(".");
String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
pos=getFormNameIndex(buffer,"\r\n".getBytes(),pos+1)+4;
int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
int len=endpos-pos;
java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filename,filenames)));
bos.write(buffer,pos,len);
bis.close();
bos.close();
}catch(Exception exc) {
throw new Exception("upload failed.");
}finally{

}
}
private int getFormNameIndex(byte[] source,byte[] formname,int start) {
int soulen=source.length;
int sealen=formname.length;
boolean hasSearch=false;
int pos=-1;
for(int i=start;i<soulen;i++) {
if(source[i]==formname[0]) {
boolean hasSear=true;
for(int k=1;k<sealen;k++) {
if(source[i+k]!=formname[k]) {
hasSear=false;
break;
}
}
hasSearch=hasSear;
}
if(hasSearch) {
pos=i;
break;
}
}
return pos;
}
}
然后编译,但是它说 Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.
error: File D:\tomcat\webapps\examples\WEB-INF\classes\learn\load\fileLoad.class does not contain type learn.load.fileLoad as expected, but type fileLoad. Please remove the file, or make sure it appears in the correct subdirectory of the class path.

气坏我了
rain_ok 2002-05-27
  • 打赏
  • 举报
回复
^_^,我搞定了,就那么简单?不见得阿,^_^
zijianyi 2002-05-27
  • 打赏
  • 举报
回复
呵呵,bean很简单呀,放在指定的文件夹里不就完了?
rain_ok 2002-05-27
  • 打赏
  • 举报
回复
java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
出错了,在编译的时候显示错误cannot resolve symbol
rain_ok 2002-05-27
  • 打赏
  • 举报
回复
哦?在哪里?
bistone 2002-05-27
  • 打赏
  • 举报
回复
去下载个JSPSMARTUPLOAD吧,很好用的,不过没有源码
rain_ok 2002-05-27
  • 打赏
  • 举报
回复
很简单,因为我不懂BEAN
saintKnight 2002-05-27
  • 打赏
  • 举报
回复
上载:
public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
try {
String contentType=req.getContentType();
int contentLength=req.getContentLength();
java.io.DataInputStream bis=new java.io.DataInputStream(req.getInputStream());
int once = 0;
int total = 0;
byte[] buffer=new byte[contentLength];
while ((total<contentLength) && (once>=0)) {
once = bis.read(buffer,total,contentLength);
total += once;
}
int boundaryStart=contentType.indexOf("boundary=");
boundaryStart=boundaryStart+"boundary=".length();
String boundary="--"+contentType.substring(boundaryStart);
int pos=getFormNameIndex(buffer,"filename=\"".getBytes(),1)+"filename=\"".length();
int posEnd=getFormNameIndex(buffer,"\"".getBytes(),pos);
String filename=new String(buffer,pos,posEnd-pos);
pos=filename.lastIndexOf(".");
String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
pos=getFormNameIndex(buffer,"\r\n".getBytes(),pos+1)+4;
int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
int len=endpos-pos;
java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
bos.write(buffer,pos,len);
bis.close();
bos.close();
}catch(Exception exc) {
throw new Exception("upload failed.");
}finally{

}
}
private int getFormNameIndex(byte[] source,byte[] formname,int start) {
int soulen=source.length;
int sealen=formname.length;
boolean hasSearch=false;
int pos=-1;
for(int i=start;i<soulen;i++) {
if(source[i]==formname[0]) {
boolean hasSear=true;
for(int k=1;k<sealen;k++) {
if(source[i+k]!=formname[k]) {
hasSear=false;
break;
}
}
hasSearch=hasSear;
}
if(hasSearch) {
pos=i;
break;
}
}
return pos;
}
JSP页面:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
fileUpLoad
</title>
</head>
<body>
<form name=fileload method=post action="fileUpLoadOp.jsp">
文件名:<input type="file" name="filename"><br>
文件夹:<input type="checkbox" name="uploadfolder" value="1">
<input type="submit" name="submit" value="上载">
</form>
</body>
</html>
JSP处理页面:
<jsp:useBean id="fload" scope="page" class="learn.load.fileLoad" />
<%
fload.uploadFile(request);
%>
zijianyi 2002-05-27
  • 打赏
  • 举报
回复
这也要呀.......为什么不要bean的呀?
saintKnight 2002-05-27
  • 打赏
  • 举报
回复
下载:
package learn.load;

/**
* Title: 学习servlet
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
import java.io.*;
public class FileDownLoad {

public FileDownLoad() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;
try {
File ff=new File(aFileName);
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));
//设置类型和头信息
response.setContentType( "application/octet-stream" ); // MIME type for pdf doc
int pos=aFileName.lastIndexOf("\\");
aFileName=aFileName.substring(pos+1);
pos=aFileName.lastIndexOf(".");
aFileName="download"+aFileName.substring(pos);
response.setHeader("Content-disposition", "attachment; filename=" +aFileName);
//传送数据
dout = new BufferedOutputStream( response.getOutputStream());
int once = 0;
int total = 0;
while ((total<length) && (once>=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
}
JSP页面:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
fileDownLoad
</title>
</head>
<jsp:useBean id="fload" scope="page" class="learn.load.FileDownLoad" />
<body>
<%
String filename=request.getParameter("filename");
if(filename!=null) fload.fileDown(filename,response);
%>
<form name=fileload method=post action="fileDownLoad.jsp">
文件名:<input type="file" name="filename"><br>
<input type="submit" name="submit" value="下载">
</form>
</body>
</html>
内容概要:本文详细探讨了机组组合优化模型的构建,旨在通过合理安排各类发电机组的启停计划和优化出力分配,实现电力系统在经济性和稳定性上的最佳平衡。文章首先介绍了电力系统的四大主要组件——传统火电机组、风电机组、光伏机组和储能系统的参数及运行特性。接着,围绕最小化系统总运行成本这一目标,设计了优化目标函数,并明确了包括功率平衡约束、机组出力上下限约束、风光发电功率约束、弃风弃光约束、爬坡速率约束、储能系统荷电状态约束、充放电功率约束和充放电互斥约束在内的多项约束条件。最后,文章列出了解机组组合优化模型所需的关键变量,如传统机组的开停状态、机组出力、启停成本、风电光伏实际出力、弃风弃光比例及储能系统的充放电功率和荷电状态,以实现系统的经济调度和可再生能源的最大化利用。 适合人群:从事电力系统研究、规划和调度工作的工程师和技术人员,以及对电力系统优化感兴趣的科研人员。 使用场景及目标:①帮助电力系统工程师理解不同类型发电机组的特点及其对系统稳定性、经济性和环保性的影响;②为制定合理的电力系统调度策略提供理论依据和技术支持;③促进可再生能源的有效整合,提高电力系统的灵活性和可靠性。 其他说明:本文提供的模型和方法不仅适用于当前的电力系统,也可为未来含高比例可再生能源接入的电力系统提供参考。文中涉及的具体数学公式和参数设定为实际应用提供了详细的指导,有助于提升电力系统的运行效率和经济效益。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧