在applet中把要上传或者下载的文件名传给servlet,然后由servlet处理是不是更简单呢?
解决方案 »
- JSP 使用FCKEditor2.6.6上传图片
- Struts2,Hibernate整合,tomcat启动出错
- tomcat中,访问html时,会走servlet引擎吗?
- 纯HTML问题!!!
- 在jsp中通过&连接符号传递参数时最大能传递多少?
- 我配好了jsp的环境在服务器上,把程序放在我做的虚拟目录里了,可是只出来程序的页面部分,执行起来却不好使?
- JSP 页面传值问题 (很急)
- 求一小例子,大家帮帮忙了
- 关于JSP链接数据库的问题,急!
- 初次写了一个类,想封装一下数据库操作,JSP执行耗时增加很多?
- Tomcat中使用taglib出错,个位高手帮忙看一下啊,感谢中........
- JavaBean放在WEB-INF\classes下但总发生错误
servlet:
ObjectInputStream ois=new ObjectInputStream(urlcon.getInputStream());
Object o = (Object)ois.readObject();applet:
URL url = new URL("servlet address?arguement=...")
URLConnection urlcon = url.openConnection();
urlcon.connect();
String fPath = "文件地址";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(fPath)));
oos.writeObject(p);
//*****************************************************************
package ***
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import bee.*;
public class chapteraddfile extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
static final int MAX_SIZE=102400*102400;
String rootPath;
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
request.setCharacterEncoding("GBK");
String name=request.getParameter("tfName");
String chapterId=request.getParameter("chapterId");
String returnUrl=request.getParameter("returnUrl");
DataInputStream in=null;
FileOutputStream fileOut=null;
String realPath=this.getServletContext().getRealPath(this.getServletName());
realPath=realPath.substring(0,realPath.lastIndexOf("\\"));
rootPath=realPath+ "\\upload\\";
try
{
String contentType=request.getContentType();
if (contentType.indexOf("multipart/form-data")>=0)
{
in=new DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
if(formDataLength>MAX_SIZE)
{
out.println("<p>上传的字节数不可以超过" + MAX_SIZE + "</p>");
return;
}
byte dataBytes[]=new byte[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead<formDataLength)
{
byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead+=byteRead;
}
String file=new String(dataBytes);
chapterId=file.substring(file.indexOf("@*@*@*@*@*")+10,file.lastIndexOf("@*@*@*@*@*"));
returnUrl=file.substring(file.indexOf("*@*@*@*@*@")+10,file.lastIndexOf("*@*@*@*@*@"));
String deleteStart=file.substring(file.indexOf("-----------------------------")+30);
name=deleteStart.substring(deleteStart.indexOf("tfName")+7,deleteStart.indexOf("-----------------------------"));
String saveFile=file.substring(file.indexOf("filename=\"")+10);
saveFile=saveFile.substring(0,saveFile.indexOf("\n"));
saveFile=saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
int lastIndex=contentType.lastIndexOf("=") ;
String boundary=contentType.substring(lastIndex+1,contentType.length());
String fileStyle=saveFile.substring(saveFile.indexOf(".")+1);
Random rdm = new Random();
int rdmNum = rdm.nextInt(10000);
Random rdm1 = new Random();
int rdmNum1 = rdm1.nextInt(10000);
String theFileName="Ben"+rdmNum1+rdmNum;
saveFile=theFileName + "." + fileStyle;
String url="upload/";
String fileName=rootPath+saveFile;
int pos;
pos=file.indexOf("filename=\"");
pos=file.indexOf("\n",pos)+1;
pos=file.indexOf("\n",pos)+1;
pos=file.indexOf("\n",pos)+1;
int boundaryLocation=file.indexOf(boundary,pos)-4;
int startPos=((file.substring(0,pos)).getBytes()).length;
int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;
File fileDir=new File(rootPath);
if(!fileDir.exists())
{
fileDir.mkdirs();
}
fileOut=new FileOutputStream(fileName);
fileOut.write(dataBytes,startPos,(endPos-startPos));
fileOut.close();
String insertSubjectFile="insert into subjectfile (name,preid,url,style) values ('" + name + "'," + chapterId + ",'" + url + "','" + fileStyle + "')";
// oracledbaction oracledbaction=new oracledbaction();
//oracledbaction.executeAction(insertSubjectFile);
bee.DBconn db = new bee.DBconn();
db.executeUpdate(insertSubjectFile);
response.sendRedirect(returnUrl); }
else
{
String content=request.getContentType();
out.println("<p>上传的数据类型不是multipart/form-data</p>");
} }
catch(Exception ex)
{
ex.printStackTrace();
}
}
//Clean up resources
public void destroy() {
}
}
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setAllowUserInteraction(false);
//urlCon.connect(); //这个需要吗?有什么问题?
那位那否给一个完整的示例,传文件+其他参数,文件名和参数的值不需要在applet里面管,applet里面的方法在调用的时候由调用者指定谢谢
URLConnection urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setRequestProperty("Content-type", "application/octest-stream");传送方法
/**
* 给Servlet传送对象
* @param output
* @param obj
* @throws IOException
*/
public void sendServletObject(Object obj) throws IOException {
OutputStream output=con.getOutputStream();//con==urlCon
ObjectOutputStream dataout = new ObjectOutputStream(output);
dataout.writeObject(obj);
dataout.flush();
dataout.close();
}
思路:使用对象流,把一个对象传送给servlet.传送的这个对象可包括其他对象,比如输入流.
public static void main(String args[]) {
try {
//建立双向通信连接
URL mtUrl = new URL("http://200.1.1.189:8080/servlet/SaveFile");
URLConnection myCon = mtUrl.openConnection();
myCon.setDoOutput(true);
//发送数据包
OutputStream outs = myCon.getOutputStream();
String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
System.out.println("==========xml信息"+xmlStr);
outs.write(xmlStr.getBytes());
outs.flush();
outs.close();
System.out.println("Send First finish.");
System.out.println(
"=====waiting for server to cope with the data!=====");
InputStream bin = myCon.getInputStream();
System.out.println(
"========server finished the datacoping============");
int length = 0;
length = myCon.getContentLength();
byte[] tempStr = new byte[length];
int res = 0;
bin.read(tempStr);
bin.close();
System.out.println("read ok. length = " + length);
System.out.println(new String(tempStr));
System.out.println("========OK.==========");
}
catch (Exception e) {
e.printStackTrace();
}
}