|
楼主 |
发表于 2011-4-6 07:20:40
|
显示全部楼层
呃嗯..?
求真相
受受梦 发表于 2011-4-6 07:02
前台
========================================================================
<script type="text/javascript">
$(document).ready(function() {
//上传图片
$("#btnUpload").click(function () {
if ($("#flUpload").val() == "") {
alert("请选择一个图片文件,再点击上传。");
return;
}
$("#form1").ajaxSubmit({
success: function (html, status) {
var result = html.replace("<pre>", "");
result = result.replace("</pre>", "");
$("#image").attr('src',result.replace("\\",'/'));
alert(result);
}
});
});
});
function setImg()
{
var isIE = document.all?true:false;
var isIE7 = isIE && (navigator.userAgent.indexOf('MSIE 7.0') != -1);
var isIE8 = isIE && (navigator.userAgent.indexOf('MSIE 8.0') != -1);
var upLoadImgFile = document.getElementById("flUpload");
var imgView = document.getElementById("image");
if(isIE){
if(isIE7 || isIE8)
{
upLoadImgFile.select();
imgView.src = document.selection.createRange().text;
document.selection.empty();
}else{ imgView.src = upLoadImgFile.value;}
}else{
imgView.src = upLoadImgFile.files.item(0).getAsDataURL();
}
}
</script>
<body>
<form method="post" id="form1" action="servlet/UploadServlet" enctype="multipart/form-data">
<input type="file" id="flUpload" name="ttt"/>
<input id="btnUpload" type="button" value="提交" onclick="ajaxFileUpload()"/>
</form>
<img id="image" />
</body>
========================================================================
后台servlet
========================================================================
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
File ttt=null;
DiskFileUpload diskFileUpload=new DiskFileUpload();
diskFileUpload.setHeaderEncoding("gbk");
try{
List<FileItem> list=diskFileUpload.parseRequest(request);
for(FileItem fileItem :list){
if(fileItem.isFormField()){
}else{
if("ttt".equals(fileItem.getFieldName())){
File remoteFile=new File(new String(fileItem.getName().getBytes(),"UTF-8"));
//System.out.println("地址:"+remoteFile.getAbsolutePath());
if(remoteFile.getAbsolutePath().endsWith(".jpg")||remoteFile.getAbsolutePath().endsWith(".png")||remoteFile.getAbsolutePath().endsWith(".gif")||remoteFile.getAbsolutePath().endsWith(".JPG")){
ttt=new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName());
ttt.getParentFile().mkdirs();
ttt.createNewFile();
InputStream ins=fileItem.getInputStream();
OutputStream ous=new FileOutputStream(ttt);
try{
byte[] buffer=new byte[1024];
int len=0;
while((len=ins.read(buffer))>-1)
{
ous.write(buffer,0,len);
}
System.out.println("已保存");
String paddess=ttt.getAbsolutePath();
//System.out.println(paddess);
System.out.println(paddess);
String picAddress=paddess.substring(paddess.indexOf("attachment"));
System.out.println(picAddress);
out.print(picAddress);
}finally{
ins.close();
ous.close();
}
}else{
System.out.println("这货不是图片");
}
}
}}}catch (Exception e) {
// TODO: handle exception
}
}
========================================================================
{:4_155:} 卡了4天换了N种方法,碰到N种奇葩的问题……终于测试成功了……过会吃好饭就结合实际需求编写了 |
|