UploadServlet.java:
import java.util.*;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;public class UploadServlet extends HttpServlet
{
protected static int UPLOAD_SIZE_MAX = 4096000;
protected static int UPLOAD_SIZE_THRESHOLD = 4096000;
protected static String TEMP_DIR = "e:\\temp";
private static Log log = LogFactory.getLog(UploadServlet.class); protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
process(request, response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
process(request, response);
}
protected void process(HttpServletRequest request, HttpServletResponse response){
try{
String test = request.getParameter("test");
System.out.println("1------"+test.length());
//test += "aa";
test = new String(test.getBytes("iso8859-1"),"utf-8");
System.out.println("2====="+test);
//saveRequest(request);
//test = test.substring(0,test.length()-2);
request.setAttribute("test",test);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/main.jsp");
dispatcher.forward(request, response);
return; }catch(Exception e){
e.printStackTrace();
}
} /** yhchen's comments: 保存查询条件 */
void saveRequest(HttpServletRequest request) {
try{
StringBuffer bf = new StringBuffer();
//PubRequest req = new PubRequest(request);
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()){
String name = (String)names.nextElement();
bf.append(name);
bf.append("=");
bf.append(new String(request.getParameter(name).getBytes("iso8859-1"),"utf-8"));
bf.append("&");
}
String requestWholeURL = "";
requestWholeURL = request.getContextPath() + request.getServletPath() + "?" + bf.toString() ;
request.setAttribute("requestWholeURL",requestWholeURL); }catch(Exception e){
System.out.println("error====" + e);
}
}
}main.jsp:
<%@page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<%
String test = (String)request.getAttribute("test");
if (test == null){
test = "\u6570\u636e\u589e";
}
%><!--form action="/upload/upload.do" enctype="multipart/form-data" method="post"-->
<form action="/upload/upload.do" method="post">
Author: <input type="text" name="test" value="<%=test%>">
<br>
Select file to upload <!--input type="file" name="filename1"-->
<br>
<input type="submit" value="upload">
</form><a href="/upload/upload.do?test=<%=test%>">aaaaaaaaaa</a>
display.jsp...............
<%@page contentType="text/html; charset=utf-8"%><%
String requestWholeURL = (String) request.getAttribute("requestWholeURL");
if (requestWholeURL != null){
%>
<a href="<%=requestWholeURL%>">testtsdfsdf</a>
<%}%>
<%
try{
if (request.getParameter("author") != null)
out.println(new String (request.getParameter("author").getBytes("iso8859-1"),"utf-8"));
} catch (Exception e)
{}
%><%
String author = (String)request.getAttribute("author");
String filePath = (String)request.getAttribute("filePath");
String fileName = (String)request.getAttribute("fileName");
String test = (String)request.getAttribute("test");
String test1 = (String)request.getAttribute("test1");
%>
Author = <%=author%><br>
Original client file: <%=fileName%><br>
File written as: "<%=filePath%>"<br>
========<%=test%>
========<%=test1%>
import java.util.*;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;public class UploadServlet extends HttpServlet
{
protected static int UPLOAD_SIZE_MAX = 4096000;
protected static int UPLOAD_SIZE_THRESHOLD = 4096000;
protected static String TEMP_DIR = "e:\\temp";
private static Log log = LogFactory.getLog(UploadServlet.class); protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
process(request, response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
process(request, response);
}
protected void process(HttpServletRequest request, HttpServletResponse response){
try{
String test = request.getParameter("test");
System.out.println("1------"+test.length());
//test += "aa";
test = new String(test.getBytes("iso8859-1"),"utf-8");
System.out.println("2====="+test);
//saveRequest(request);
//test = test.substring(0,test.length()-2);
request.setAttribute("test",test);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/main.jsp");
dispatcher.forward(request, response);
return; }catch(Exception e){
e.printStackTrace();
}
} /** yhchen's comments: 保存查询条件 */
void saveRequest(HttpServletRequest request) {
try{
StringBuffer bf = new StringBuffer();
//PubRequest req = new PubRequest(request);
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()){
String name = (String)names.nextElement();
bf.append(name);
bf.append("=");
bf.append(new String(request.getParameter(name).getBytes("iso8859-1"),"utf-8"));
bf.append("&");
}
String requestWholeURL = "";
requestWholeURL = request.getContextPath() + request.getServletPath() + "?" + bf.toString() ;
request.setAttribute("requestWholeURL",requestWholeURL); }catch(Exception e){
System.out.println("error====" + e);
}
}
}main.jsp:
<%@page contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>File Upload</title>
</head>
<body>
<%
String test = (String)request.getAttribute("test");
if (test == null){
test = "\u6570\u636e\u589e";
}
%><!--form action="/upload/upload.do" enctype="multipart/form-data" method="post"-->
<form action="/upload/upload.do" method="post">
Author: <input type="text" name="test" value="<%=test%>">
<br>
Select file to upload <!--input type="file" name="filename1"-->
<br>
<input type="submit" value="upload">
</form><a href="/upload/upload.do?test=<%=test%>">aaaaaaaaaa</a>
display.jsp...............
<%@page contentType="text/html; charset=utf-8"%><%
String requestWholeURL = (String) request.getAttribute("requestWholeURL");
if (requestWholeURL != null){
%>
<a href="<%=requestWholeURL%>">testtsdfsdf</a>
<%}%>
<%
try{
if (request.getParameter("author") != null)
out.println(new String (request.getParameter("author").getBytes("iso8859-1"),"utf-8"));
} catch (Exception e)
{}
%><%
String author = (String)request.getAttribute("author");
String filePath = (String)request.getAttribute("filePath");
String fileName = (String)request.getAttribute("fileName");
String test = (String)request.getAttribute("test");
String test1 = (String)request.getAttribute("test1");
%>
Author = <%=author%><br>
Original client file: <%=fileName%><br>
File written as: "<%=filePath%>"<br>
========<%=test%>
========<%=test1%>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货