第一副图:
点击浏览后,出现第二幅图:这个浏览的代码怎么写啊,怎么能浏览到系统中的文件的呢???如果可能,给一个完整的代码最好(包括上传的)!
点击浏览后,出现第二幅图:这个浏览的代码怎么写啊,怎么能浏览到系统中的文件的呢???如果可能,给一个完整的代码最好(包括上传的)!
解决方案 »
- 哥哥,姐姐们。帮小弟我看看这个问题。
- 连接占线导致另一个 hstmt是怎么回事啊?主要是什么原因造成的啊?
- 怎样可以return mapping.findForward("xxx");后地址栏不为xxx.do,而是我想要的xxx.jsp?
- 一个字段存储信息的问题
- 在jsp里做在线播放
- 还是英语翻译,都是单句的,关于计算机的,谢谢大家
- J2EE应用中与Oracle数据库的连接?
- taglib问题求救,问了好多地方都没有人回,请大家帮帮帮忙!
- gary_shi(Gary Shi)兄进来说话,谢谢。
- 请看一下我的程序错在哪里,好吗?
- JSP模板技术JDynamiTe详细设计思路
- hsqldb使用问题
就可以实现lz需要的两步
但是具体的实现机制 可以使用 fileupload.jar包来实现
或者使用smartupload包
具体实现,goole搜索
JSP页面应该好做,我把Servlet类给你吧!文件名是UploadServlet.java 祝你成功!
package cServlet;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;/**
*内容可以根据实际需要做适当修改
* @author KingKong
*/
public class UploadServlet extends HttpServlet {
public static final int MAX_SIZE=1024*1024*2;//上传文件的大小限制,最大2M
public static final String FILE_DIR="D:/上传的文件/";//上传文件存储路径
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<style type=text/css><!--body { background-image: url(img/8.jpg);}--></style>");
out.println("<html>");
out.println("<head>");
out.println("<title>文件上传</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
} // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
} /**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
DataInputStream dis=new DataInputStream(req.getInputStream());
res.setCharacterEncoding("UTF-8");
PrintStream ps=new PrintStream(res.getOutputStream());
String content=req.getContentType();
if(content!=null && content.indexOf("multipart/form-data")!=-1){
int reqSize=req.getContentLength();
byte[] data=new byte[reqSize];
int bytesRead=0;
int totalBytesRead=0;
int sizeCheck=0;
while(totalBytesRead<reqSize){
sizeCheck=totalBytesRead+dis.available();
if(sizeCheck>MAX_SIZE){
ps.println("<h1 align=center>对不起,文件太大不能上传……</h1>");
return;
}
bytesRead=dis.read(data,totalBytesRead,reqSize);
totalBytesRead+=bytesRead;
}
String dataString=new String(data,"UTF-8");
dataString=dataString.substring(dataString.indexOf("filename=\""));
String filePath=dataString.substring(0, dataString.indexOf("Content-Type:"));
dataString=dataString.substring(dataString.indexOf("Content-Type:")+1);
dataString=dataString.substring(dataString.indexOf("\n")+1);
dataString=dataString.substring(2, dataString.lastIndexOf("\n")-1);
dataString=dataString.substring(0, dataString.lastIndexOf("\n")-1);
writeFile(dataString.getBytes("UTF-8"),FILE_DIR+getFileName(filePath));
ps.println("<h1 align=center>恭喜你,文件上传成功……</h1>");
}
}
public boolean writeFile(byte[] data,String path){
File f=null;
FileOutputStream fos=null;
try{
f=new File(path);
f.createNewFile();
fos=new FileOutputStream(f);
fos.write(data, 0, data.length);
}catch(FileNotFoundException e){
return false;
}catch(IOException e){
return false;
}finally{
try{
fos.close();
}catch(IOException e){
return false;
}
}
return true;
} public String getFileName(String arg){
String path=arg.substring(arg.indexOf("\"")+1,arg.lastIndexOf("\"") );
path=path.substring(path.lastIndexOf("\\")+1);
return path;
} /**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>}