📄 goodimageadd.jsp
字号:
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="gb2312"%>
<%@ page import="eshopsys.goodtype.biz.*" %>
<%@ page import="eshopsys.goodtype.model.*" %>
<%@ page import="eshopsys.good.biz.*" %>
<%@ page import="eshopsys.good.model.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("gb2312");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'showGoodTypeBrowse.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String fileName=null;
String filePath="E:/Struts/Eshop/WebRoot/SysManager/Good/GoodImage";
try{
//用客户的session对象Id建立一个临时文件。
String tempFileName=(String)session.getId();
//建立临时文件f1
File f1=new File(filePath,tempFileName);
FileOutputStream o=new FileOutputStream(f1);
//将客户上传的全部信息存入f1
InputStream in=request.getInputStream();
byte b[]=new byte[request.getContentLength()];
int n;
while((n=in.read(b))!=-1)
{o.write(b,0,n);
}
o.close();
in.close();
//读取临时文件f1,从中获取上传文件的名字和上传文件的内容
RandomAccessFile random=new RandomAccessFile(f1,"r");
//读取出f1的第2行,分析出上传的文件名称
int second=1;
String secondLine=null;
while(second<=2)
{secondLine=random.readLine();
second++;
}
//获取第2行中目录符号"\"最后出现的位置
int position=secondLine.lastIndexOf('\\');
fileName=secondLine.substring(position+1,secondLine.length()-1);
random.seek(0);
//获取第4行回车符号的位置
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1 && (forth<=4))
{if(n=='\n')
{forthEndPosition=random.getFilePointer();
forth++;
}
}
//根据客户上传文件名称,将文件存入磁盘
byte cc[]=fileName.getBytes("ISO-8859-1");
fileName=new String(cc);
File f2=new File(filePath,fileName);
//session.setAttribute("Name",fileName);
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定出文件f1中包含客户上传的文件内容的最后位置,即倒数第6行
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6))
{mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n')
{endPosition=random.getFilePointer();
j++;
}
}
//将random流文件f1的第4行结束位置
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//从f1读取客户上传的文件内容存入f2(第4行结束位置到第6行之间的内容)
while(startPoint<endPosition-1)
{n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
f1.delete();
////////////////////////////////////////////////////
GoodEntity good=(GoodEntity)session.getAttribute("good");
good.setGoodImage(fileName);
GoodBiz goodbiz=new GoodBiz();
goodbiz.updateGood(good);
goodbiz.CloseConnection();
session.setAttribute("good",null);
response.sendRedirect("/Eshop/SysManager/Good/goodAddView.jsp");
////////////////////////////////////////////////////
}
catch(IOException ee){}
//out.println("文件已经上传!");
//out.println(fileName);
%>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -