_____________我的代码——————————————————————————<%@ page language="java"contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>保存上传文件</title>
</head> <%
String msg = "";
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp");//FileUpload fu = new FileUpload();
// 设置允许用户上传文件大小,单位:字节
//fu.setSizeMax(10000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
//fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//fu.setRepositoryPath("Revenue");
//开始读取上传信息
List fileItems = fu.parseRequest(request);%><body bgcolor="#FFFFFF"text="#000000" leftmargin="0"topmargin="40"marginwidth="0" marginheight="0">
<font size="6"color="blue">文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width="100%">
<tr>
<td bgcolor="#008080">文件名</td>
<td bgcolor="#008080">大小</td>
</tr><%
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
%><tr>
<td><%=item.getName()%></td>
<td><%=item.getSize()%></td>
</tr><% //保存上传的文件到指定的目录
//name = name.replace(':','_');
//name = name.replace('\\','_');
item.write("\\" + name);
}
}%>
</table><br/><br/>
<a href="upload.html">返回上传页面</a>
</center>
</body>
</html>
提示出错的-----------------------------------An error occurred at line: 63 in the jsp file: /save.jsp
Generated servlet error:
/home/dream/src/jakarta-tomcat-5.0.27/work/Catalina/localhost/Revenue/org/apache/jsp/save_jsp.java:119: unclosed string literal
item.write("\" + name);
^
An error occurred at line: 63 in the jsp file: /save.jsp
Generated servlet error:
/home/dream/src/jakarta-tomcat-5.0.27/work/Catalina/localhost/Revenue/org/apache/jsp/save_jsp.java:120: ')' expected
^
2 errors
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>保存上传文件</title>
</head> <%
String msg = "";
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp");//FileUpload fu = new FileUpload();
// 设置允许用户上传文件大小,单位:字节
//fu.setSizeMax(10000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
//fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//fu.setRepositoryPath("Revenue");
//开始读取上传信息
List fileItems = fu.parseRequest(request);%><body bgcolor="#FFFFFF"text="#000000" leftmargin="0"topmargin="40"marginwidth="0" marginheight="0">
<font size="6"color="blue">文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width="100%">
<tr>
<td bgcolor="#008080">文件名</td>
<td bgcolor="#008080">大小</td>
</tr><%
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
%><tr>
<td><%=item.getName()%></td>
<td><%=item.getSize()%></td>
</tr><% //保存上传的文件到指定的目录
//name = name.replace(':','_');
//name = name.replace('\\','_');
item.write("\\" + name);
}
}%>
</table><br/><br/>
<a href="upload.html">返回上传页面</a>
</center>
</body>
</html>
提示出错的-----------------------------------An error occurred at line: 63 in the jsp file: /save.jsp
Generated servlet error:
/home/dream/src/jakarta-tomcat-5.0.27/work/Catalina/localhost/Revenue/org/apache/jsp/save_jsp.java:119: unclosed string literal
item.write("\" + name);
^
An error occurred at line: 63 in the jsp file: /save.jsp
Generated servlet error:
/home/dream/src/jakarta-tomcat-5.0.27/work/Catalina/localhost/Revenue/org/apache/jsp/save_jsp.java:120: ')' expected
^
2 errors
解决方案 »
- 部署项目后重启Weblogic服务报错!!!!!
- C3P0报错
- 网站把我的账号换掉了
- 想请问插入问题,数据插入有问题
- 有关网页抓取,大家帮帮小弟我
- 上传问题 急!
- jbuilder中建立jsp页面出现的问题
- String send_id = session.getAttribute("LoginName");有錯誤,但<%=session.getAttribute("LoginName") %>卻是對的,為什麼
- tomcat报错信息,请各位帮我看一下。
- 开发使用myeclipse/eclipse+win7==一场噩梦?
- 在java中,如何将string型转换为float型,有没有现成的函数啊
- 怎样实现从html格式网页中获取自己想要的信息
<%!
/**
* 上传一个文件
*/
public byte[] uploadFile(HttpServletRequest request, HashMap field)
{
byte[] file = null;
try
{
int contentLength = request.getContentLength();
ServletInputStream in = request.getInputStream();
file = new byte[contentLength];
byte[] line = new byte[4 * 1024];
int i = in.readLine(line, 0, line.length);
/** 只有换行和回车 */
if (i < 3)
{
return null;
}
/**
* 第一行是分割行
* 如:-----------------------------7d519338250c78
*/
int boundLen = i - 2;
String bound = new String(line, 0, boundLen);
/** 未到最后行 */
while (i != -1)
{
String stringLine = new String(line, 0, i, "gb2312");
/** 文件描述开始 */
if (stringLine.indexOf("filename=\"") != -1)
{
int start = 0;
field.put("file",getFilename(new String(line,0,i-2,"gb2312")));
/** 读取下一行文件信息(如:Content-Type: text/css) */
i = in.readLine(line, 0, line.length);
/** 读取文件内容开始前的空行 */
i = in.readLine(line, 0, line.length);
/** 开始读取文件内容 */
i = in.readLine(line, 0, line.length);
stringLine = new String(line, 0, i, "gb2312");
while (i != -1 && !stringLine.startsWith(bound))
{
System.arraycopy(line, 0, file, start, i);
start += i;
i = in.readLine(line, 0, line.length);
stringLine = new String(line, 0, i, "gb2312");
}
}
else
{
if(stringLine.startsWith("Content-Disposition: form-data; name=\""))
{
int pos = stringLine.indexOf("name=\"");
String fieldName = stringLine.substring(pos + 6,stringLine.length() - 3);
//读取空行
i = in.readLine(line, 0, line.length);
i = in.readLine(line, 0, line.length);
stringLine = new String(line, 0, i-2, "gb2312");
String fieldValue = stringLine;
field.put(fieldName,fieldValue);
}
} i = in.readLine(line, 0, line.length);
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
}
return file;
}%>然后在一个新页面用流输出到JSP里就可以了:
<%@page buffer="none"%>
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@include file="inc/func.jsp" %>
<%@include file="inc/getstring.jsp" %>
<%
request.setCharacterEncoding("gb2312");HashMap fhm = new HashMap();
byte[] file = uploadFile(request,fhm);
String filename = (String)fhm.get("file");
String directory =(String)fhm.get("directory");
File savedFile = null;
if(filename != null && directory != null)
{
try
{
savedFile = new File(directory,filename);
if(savedFile.exists())
{
out.println("<script language=\"JavaScript\">");
out.println("if (!confirm(\"您上传的文件已经存在是否覆盖?\")) {");
out.println("history.back();}");
out.println("</script>");
}
else
{
savedFile.createNewFile();
}
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(savedFile));
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(file));
int c = 0;
while((c = in.read()) > -1)
{
os.write(c);
}
os.flush();
os.close();
in.close();
}catch(Exception e)
{
System.out.println(e);
}
}
%>
<script language="JavaScript">
window.opener.parent.leftFrame.location="menu.jsp";
window.close();
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body>
</body>
</html>
<%@ page import="java.net.*,java.io.*" %>
<html>
<head>
<title>发送Delete</title>
</head>
<body><%
String s = request.getParameter("fileName");
String file="http://localhost:8081/china/servlet/doput";
System.out.println(file);
try{
URL doUp = new URL(file);
HttpURLConnection doUpServletConn = (HttpURLConnection)doUp.openConnection();
doUpServletConn.setRequestMethod("PUT");
doUpServletConn.setDoOutput(true);
OutputStream outPutStream=doUpServletConn.getOutputStream();
FileInputStream inPutStream = new FileInputStream(s);
int b;
while((b = inPutStream.read())!=-1)
outPutStream.write(b);
doUpServletConn.getInputStream();
inPutStream.close();
outPutStream.close();
}catch(Exception ex){
ex.printStackTrace();
} %>
</body>
</html>-- Servlet 文件 doput.java --import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class doput extends HttpServlet{
private static final String CONTENT_TYPE = "text/html;charset=GBK";
// public void init() throws ServletException{}
public void doPut(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
try{
InputStream reqInput=req.getInputStream();
BufferedInputStream inStream=new BufferedInputStream(reqInput);
File outputFile=new File("g:/temp/a.jpg");
FileOutputStream outStream = new FileOutputStream(outputFile);
int b;
while((b=inStream.read())!=-1){
outStream.write(b);
}
inStream.close();
outStream.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
// public void destroy()
{
}
}