file控件点击浏览按钮,获取他选择文件的路径,然后根据这个路径读取文件里面的内容,将他显示到文本框里,怎么做啊求教实现的功能是 点击浏览的时候你选择一个文件,这时候这个file控件里就有了选择的文件的路径,想通过这个路径,把选择的文件的内容读取到 文本框中,这个要怎么实现,求解啊~~
解决方案 »
- 利用inputstream读取文件大小的时候报错
- 把GB2312改成UTF-8网页乱码
- Servlet超链接传参数的问题,求教!
- sql server是windows身份验证在JSP中该怎样写代码?
- 请问在java里如何调用org.apache.xalan.xslt.Process的功能生成一个HTML文本,跟XML和XSLT有关?
- equals如何判断长字符串相等
- tomcat5.5 ssi 中文乱码
- 用java运行时出现exception的输入显示
- tomcat5.019怪现象!
- 打开Excel文件!
- 学习hibernate时出现的问题
- eclipse 安装 activiti 插件报错! 求解决!
参照 http://hi.baidu.com/chjie219/blog/item/73429a824d4ed698f603a6ca.html
public class ImageServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
MultipartRequest mr = null;
int maxPostSize = 1*100*1024*1024;
String dir = getServletConfig().getServletContext().getRealPath("/uploadImage");
mr = new MultipartRequest(request,dir,maxPostSize,"utf-8");
Enumeration files = mr.getFileNames();
String fileName ="";
String path = "";
while(files.hasMoreElements()){
fileName = files.nextElement().toString();
path = mr.getFilesystemName(fileName);
File f = mr.getFile(fileName);
if(f==null){
throw new ServletException("file is null");
} Date dt = new Date(System
.currentTimeMillis());
SimpleDateFormat fmt = new SimpleDateFormat(
"yyyyMMddHHmmssSSS");
String time = fmt.format(dt);
String newName = time+f.getName().substring(path.lastIndexOf('.'));
File newFile = new File(dir+"/"+newName);
f.renameTo(newFile);
System.out.println("newName:"+newName);
System.out.println("filename:"+newFile.getName());
System.out.println("path:"+newFile.getPath());
}
PrintWriter out = response.getWriter();
out.print("上传成功!");
}}